install node and npm using nvm

在 Mac OSX 上node0.12.2版本升级到8.10.0之后,执行nodemon时抛出以下错误:

exception in nodemon killing nodeError: Cannot find module 'internal/util/types'    at Function.Module._resolveFilename (module.js:513:15)    at Function.Module._load (module.js:463:25)    at Module.require (module.js:556:17)    at require (internal/module.js:11:18)    at evalmachine.<anonymous>:31:26    at Object.<anonymous> (/usr/local/lib/node_modules/nodemon/node_modules/update-notifier/node_modules/configstore/node_modules/graceful-fs/fs.js:11:1)    at Module._compile (module.js:612:30)    at Object.Module._extensions..js (module.js:623:10)    at Module.load (module.js:531:32)    at tryModuleLoad (module.js:494:12)

网上查了一些资料,最简单的方式就是完全卸载旧版本,并将系统里的node_modules清理干净,再重新安装就可以。

清理旧版本 node

Mac OSX 上不建议使用Homebrew来安装node,将原来安装的所有相关程序和类库全部清理干净,如果不清理这些旧的类库,使用NVM时会碰到如下一些警告信息,因为NVM不支持Homebrew方式安装的nodenpm

nvm is not compatible with the npm config "prefix" option: currently set to "/Users/test/.nvm/versions/node/v0.12.2"
Run nvm use --delete-prefix v8.10.0 to unset it.

 brew uninstall npm brew uninstall node [ -s /usr/local/bin/node ] && rm -f /usr/local/bin/node [ -s /usr/local/bin/npm ] && rm -f /usr/local/bin/npm rm -rf /usr/local/lib/node_modules

安装 NVM

 cd ~ git clone https://github.com/creationix/nvm.git .nvm cd ~/.nvm git checkout v0.33.8 . nvm.sh

安装好之后,在~/.bashrc~/.bash_profile或者~/.zshrc文件之一中加以下配置:

export NVM_DIR="$HOME/.nvm"[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
 nvm helpNode Version ManagerNote: <version> refers to any version-like string nvm understands. This includes:  - full or partial version numbers, starting with an optional "v" (0.10, v0.1.2, v1)  - default (built-in) aliases: node, stable, unstable, iojs, system  - custom aliases you define with `nvm alias foo` Any options that produce colorized output should respect the `--no-colors` option.Usage:  nvm --help                                Show this message  nvm --version                             Print out the installed version of nvm  nvm install [-s] <version>                Download and install a <version>, [-s] from source. Uses .nvmrc if available    --reinstall-packages-from=<version>     When installing, reinstall packages installed in <node|iojs|node version number>    --lts                                   When installing, only select from LTS (long-term support) versions    --lts=<LTS name>                        When installing, only select from versions for a specific LTS line    --skip-default-packages                 When installing, skip the default-packages file if it exists    --latest-npm                            After installing, attempt to upgrade to the latest working npm on the given node version  nvm uninstall <version>                   Uninstall a version  nvm uninstall --lts                       Uninstall using automatic LTS (long-term support) alias `lts/*`, if available.  nvm uninstall --lts=<LTS name>            Uninstall using automatic alias for provided LTS line, if available.  nvm use [--silent] <version>              Modify PATH to use <version>. Uses .nvmrc if available    --lts                                   Uses automatic LTS (long-term support) alias `lts/*`, if available.    --lts=<LTS name>                        Uses automatic alias for provided LTS line, if available.  nvm exec [--silent] <version> [<command>] Run <command> on <version>. Uses .nvmrc if available    --lts                                   Uses automatic LTS (long-term support) alias `lts/*`, if available.    --lts=<LTS name>                        Uses automatic alias for provided LTS line, if available.  nvm run [--silent] <version> [<args>]     Run `node` on <version> with <args> as arguments. Uses .nvmrc if available    --lts                                   Uses automatic LTS (long-term support) alias `lts/*`, if available.    --lts=<LTS name>                        Uses automatic alias for provided LTS line, if available.  nvm current                               Display currently activated version  nvm ls                                    List installed versions  nvm ls <version>                          List versions matching a given <version>  nvm ls-remote                             List remote versions available for install    --lts                                   When listing, only show LTS (long-term support) versions  nvm ls-remote <version>                   List remote versions available for install, matching a given <version>    --lts                                   When listing, only show LTS (long-term support) versions    --lts=<LTS name>                        When listing, only show versions for a specific LTS line  nvm version <version>                     Resolve the given description to a single local version  nvm version-remote <version>              Resolve the given description to a single remote version    --lts                                   When listing, only select from LTS (long-term support) versions    --lts=<LTS name>                        When listing, only select from versions for a specific LTS line  nvm deactivate                            Undo effects of `nvm` on current shell  nvm alias [<pattern>]                     Show all aliases beginning with <pattern>  nvm alias <name> <version>                Set an alias named <name> pointing to <version>  nvm unalias <name>                        Deletes the alias named <name>  nvm install-latest-npm                    Attempt to upgrade to the latest working `npm` on the current node version  nvm reinstall-packages <version>          Reinstall global `npm` packages contained in <version> to current version  nvm unload                                Unload `nvm` from shell  nvm which [<version>]                     Display path to installed node version. Uses .nvmrc if available  nvm cache dir                             Display path to the cache directory for nvm  nvm cache clear                           Empty cache directory for nvmExample:  nvm install 8.0.0                     Install a specific version number  nvm use 8.0                           Use the latest available 8.0.x release  nvm run 6.10.3 app.js                 Run app.js using node 6.10.3  nvm exec 4.8.3 node app.js            Run `node app.js` with the PATH pointing to node 4.8.3  nvm alias default 8.1.0               Set default node version on a shell  nvm alias default node                Always default to the latest available node version on a shellNote:  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

查看长期维护的版本:

 nvm ls-remote --lts         v4.8.6   (LTS: Argon)         v4.8.7   (LTS: Argon)         v4.9.0   (LTS: Argon)         v4.9.1   (Latest LTS: Argon)        v6.13.0   (LTS: Boron)        v6.13.1   (LTS: Boron)        v6.14.0   (LTS: Boron)        v6.14.1   (Latest LTS: Boron)         v8.9.4   (LTS: Carbon)        v8.10.0   (LTS: Carbon)        v8.11.0   (LTS: Carbon)        v8.11.1   (Latest LTS: Carbon)

安装 node

安装最新版本并设置为默认版本:

 nvm install v9.10.1Downloading and installing node v9.10.1...######################################################################## 100.0%Computing checksum with shasum -a 256Checksums matched!Now using node v9.10.1 (npm v5.6.0) nvm alias default v9.10.1default -> v9.10.1

nodenpm安装位置和版本号:

 which node~/.nvm/versions/node/v9.10.1/bin/node which npm~/.nvm/versions/node/v9.10.1/bin/npm node -vv9.10.1 npm -v5.6.0

使用 npm 国内镜像

添加~/.npmrc文件:

 echo "registry=https://registry.npm.taobao.org/" > ~/.npmrc

安装 node 软件

因为使用NVM管理,所有的 node 软件安装在个人目录下,所以不用sudo安装应用程序。

 npm install -g hexo-clihexo-cli@1.1.0added 219 packages from 415 contributors in 11.346s npm install -g pm2 nodemon+ nodemon@1.17.3+ pm2@2.10.2added 916 packages in 28.045s

References

  1. Error: Cannot find module 'internal/util/types'
  2. github.com nvm
  3. 正确的安装和使用nvm