Is there any way how to run “nvm use” automatically in “prestart” npm script?

I would like to have automatically invoke "nvm use" when I run "npm start". So I come up with this solution:


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




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

    nvm use

This works and switch between nvm versions console output is:

> sh

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

> [email protected] start /app-path/
> nodemon index.js

But when I call form index.js "console.log(process.versions);" nvm script is executed probably in different process so output is:

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

Any suggestions how to deal with this in proper way?




Generally on a Mac, the file is located in your home path. Use the $HOME variable if you have multiple Mac users working on the code.

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

I would've added this as a comment to the above response, but I'm not allowed :(


Your package.json could look like

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

To explain. The "start" line is a single shell instance. So you have to have nvm initialize the PATH in that shell instance. Also, nvm is a shell function not an executable shell script. The nvm function lives in the shell instance, and is created by sourcing the file.

Sorry for the edits cuz I didn't test my first two.


Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.