安装

nvm是node的包版本管理工具,github地址如下:nvm

安装命令

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.8/install.sh | bash
nvm //检查nvm是否安装成功
-bash: nvm: command not found  //boom,失败了

解决nvm command not found问题

进入.nvm文件夹,新建.bash_profile:

touch .bash_profile //新建文件
open .bash_profile //打开文件

在里面copy如下内容:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm

关闭文件,然后执行这个文件:

source .bash_profile

执行完毕,我们再看看是否安装成功:

nvm --version

输出:

0.33.8

安装成功。

卸载:

$ nvm use system
$ npm uninstall -g a_module

安装问题:

1. How to recover from broken old npm.

Old npm won’t work with new node:

npm -v

/Users/marcus/.nvm/v0.10.48/lib/node_modules/npm/bin/npm-cli.js:79
      let notifier = require('update-notifier')({pkg})
          ^^^^^^^^
SyntaxError: Unexpected identifier
    at Module._compile (module.js:439:25)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:945:3

Install old node:

nvm install 4.8.4
npm i -g npm
/Users/marcus/.nvm/versions/node/v4.8.4/bin/npm -> /Users/marcus/.nvm/versions/node/v4.8.4/lib/node_modules/npm/bin/npm-cli.js
/Users/marcus/.nvm/versions/node/v4.8.4/bin/npx -> /Users/marcus/.nvm/versions/node/v4.8.4/lib/node_modules/npm/bin/npx-cli.js
npm@6.0.1 /Users/marcus/.nvm/versions/node/v4.8.4/lib/node_modules/npm

Now new npm won’t work with old node:

npm -v
/Users/marcus/.nvm/versions/node/v4.8.4/lib/node_modules/npm/bin/npm-cli.js:79
      let notifier = require('update-notifier')({pkg})
      ^^^

SyntaxError: Block-scoped declarations (let, const, function, class) not yet supported outside strict mode
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:140:18)
    at node.js:1043:3

Reinstall node 10:

nvm install 10
Now using node v10.1.0 (npm v5.6.0)

I don’t know why npm somehow went to 5.6.0 there, but now update it:

npm install -g npm
/Users/marcus/.nvm/versions/node/v10.1.0/bin/npx -> /Users/marcus/.nvm/versions/node/v10.1.0/lib/node_modules/npm/bin/npx-cli.js
/Users/marcus/.nvm/versions/node/v10.1.0/bin/npm -> /Users/marcus/.nvm/versions/node/v10.1.0/lib/node_modules/npm/bin/npm-cli.js
+ npm@6.0.1
added 246 packages, removed 40 packages and updated 125 packages in 22.342s

And now it’s happy:

npm -v
6.0.1

You might want to remove old node versions now.

参考:

  1. npm upgrade prompt should show warning when current node version is not supported in newer version
  2. nvm安装、解决nvm command not found问题、卸载
  3. linux下安装nvm
  4. ubuntu上安装并使用nvm管理node版本