我想在运行"npm start"时自动调用"nvm use".所以我想出了这个解决方案:
的package.json
"scripts": {
"prestart": "sh test.sh",
"start": "nodemon index.js"
}
Run Code Online (Sandbox Code Playgroud)
.nvmrc
4
Run Code Online (Sandbox Code Playgroud)
test.sh
#!/bin/bash
if [ -d ~/.nvm ]
then
source ~/.nvm/nvm.sh
nvm use
fi
Run Code Online (Sandbox Code Playgroud)
这工作并在nvm版本控制台输出之间切换是:
> 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
Run Code Online (Sandbox Code Playgroud)
但是当我调用form index.js"console.log(process.versions);"时 nvm脚本可能在不同的进程中执行,因此输出为:
{ 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' }
Run Code Online (Sandbox Code Playgroud)
有任何建议如何正确处理这个问题?
谢谢