背景
由于一个项目依赖的node
版本比较老,所以在打包更新之前需要先指定一下node
版本,本来是想在shell
脚本中直接使用nvm
来指定node
版本的,然后错误就来了,会提示nvm: command not found
。
方法
需要在使用nvm
命令之前重新source
一下nvm.sh
,
例如:
. ~/.nvm/nvm.sh
nvm use 8.9.4
如果是用brew
安装的,把路径改一下就可以了,例如:
. $(brew --prefix nvm)/nvm.sh
nvm use 8.9.4
原因
The reason that nvm is a shell function and not a script is because it works by changing the environment of your current shell. This is a major design decision and the thing that makes nvm different from n and nave. n works by creating global symlinks to different node versions. nave works by creating a new subshell with the desired env modifications. I decided to go the less intrusive route and only modify the current shell. Mostly it just mucks around with $PATH so that the desired version of node is first on the path. Other shells can concurrently use their own local node version unmolested. I do this without the overhead of creating new subshells or changing global paths.
Only shell functions can do what I want. The major drawback obviously is it confuses people who think nvm.sh is a shell script meant to be run in a subshell like every other sh file they've seen. I'm sorry for the confusion but it is what it is. Subshells can't modify their parent's environment.
这个大概是原因吧,来自creationix comment