Is there a way to run "nvm use" automatically in a "preart" npm script?

I would like to automatically call "use nvm" when starting "npm start". So I came up with this solution:

package.json

"scripts": {
"prestart": "sh test.sh",
"start": "nodemon index.js"
}

.nvmrc

4

test.sh

#!/bin/bash

if [ -d ~/.nvm ]
  then
    source ~/.nvm/nvm.sh

    nvm use
fi

This works and switches between nvm console versions:

> sh test.sh

Found '/my-user-path/.nvmrc' with version <4>
Now using node v4.2.2 (npm v2.14.7)

> app@1.0.0 start /app-path/
> nodemon index.js

But when I call the form index.js "console.log (process.versions);" The nvm script is probably running in another process, so the output is:

{ http_parser: '2.6.0',
  node: '5.1.0',
  v8: '4.6.85.31',
  uv: '1.7.5',
  zlib: '1.2.8',
  ares: '1.10.1-DEV',
  icu: '56.1',
  modules: '47',
  openssl: '1.0.2d' }

Any suggestions on the correct solution to this issue?

thanks

+4
source share
2 answers

Mac nvm.sh . $HOME, Mac, .

"scripts": {
    "prestart": "source $HOME/.nvm/nvm.sh; nvm use"
}

, : (

+4

package.json

"scripts": {
"start": "source /whereever/located/nvm.sh; nvm use; nodemon index.js"
}

. "start" - . nvm PATH . , nvm - , script. nvm nvm.sh.

, .

+1

Source: https://habr.com/ru/post/1620215/


All Articles