mac 安装node.js

安裝 NVM (Node Version Manager)

使用 brew 安裝 NVM

$ brew install nvm

將下列指令加入 .bash_profile(或 .bashrc)文件

export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

重新載入 .bash_profile 設定

$ source .bash_profile

测试 nvm 指令

kqzc:.nvm williesliang$ nvm

Node Version Manager

Note: <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 nvm

Example:
  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 shell

Note:
  to remove, delete, or uninstall nvm - just remove the `$NVM_DIR` folder (usually `~/.nvm`)

使用 NVM 安裝 Node.js

找出目前所有可安裝的 Node.js 版本

$ nvm ls-remote
    v0.11.8
    v0.11.9
    v0.11.10
    v0.11.11
    v0.11.12
    v0.11.13
    v0.11.14
    v0.11.15
    v0.11.16
    ...

安裝 Node.js (5.0.0)

$ nvm install 5.0.0

指定nvm使用的 Node.js版本

nvm use 5.0.0

预设使用 5.0.0 版本,否則每次重新連線登入,還需要重新 nvm use 一次

nvm alias default 5.0.0

列出所有安裝的版本

$ nvm ls

->       system
node -> stable (-> N/A) (default)
iojs -> N/A (default)
lts/* -> lts/boron (-> N/A)
lts/argon -> v4.8.4 (-> N/A)
lts/boron -> v6.11.3 (-> N/A)

測試 Node.js

$ node -v
v4.4.3

测试npm

$ npm -v
2.15.1

nvm 重启后终端 node ,npm 失效的解决方案

如果你使用了上面的 brew 的安装方法
当安装成功后,请按照如下操作设置你的 shell 配置文件

brew info nvm

你会看到下面的输出帮助信息

nvm: stable 0.26.1 (bottled), HEAD
Manage multiple Node.js versions
https://github.com/creationix/nvm
/usr/local/Cellar/nvm/0.26.1 (3173 files, 56M) *
  Poured from bottle
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/nvm.rb
==> Caveats
Add NVM's working directory to your $HOME path (if it doesn't exist):

  mkdir ~/.nvm

Copy nvm-exec to NVM's working directory

  cp $(brew --prefix nvm)/nvm-exec ~/.nvm/

Add the following to $HOME/.bashrc, $HOME/.zshrc, or your shell's
equivalent configuration file:

  export NVM_DIR=~/.nvm
  source $(brew --prefix nvm)/nvm.sh

Type `nvm help` for further information.

Bash completion has been installed to:
  /usr/local/etc/bash_completion.d

我们需要按照上面提供的方法,首先在用户更目录下 创建 .nvm 文件

mkdir ~/.nvm

然后把 nvm-exec 文件拷贝到你刚才新建的 .nvm 目录下

cp $(brew --prefix nvm)/nvm-exec ~/.nvm/

然后去编辑你的bash配置文件 $HOME/.bashrc ,如果你使用zsh那么编辑$HOME/.zshrc配置文件

nano ~/.bashrc

nano ~/.zshrc

把下面的内容粘贴进去

export NVM_DIR=~/.nvm
source $(brew --prefix nvm)/nvm.sh

最后让你的 shell 配置及时生效

source ~/.bashrc

`source ~/.zshrc’

最后你就不会再出现关闭终端重启,或者重启机器发现 node ,npm 等系统环境变量失效的问题了.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值