Categories
bash javascript node.js nvm

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

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

package.json file

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

.nvmrc file

4

test.sh file

#!/bin/bash
if [ -d ~/.nvm ]
then
source ~/.nvm/nvm.sh
nvm use
fi

This works and switches between nvm versions console output is:

> sh test.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: '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 how to deal with this in a proper way?

Thanks