安装node和npm是前端开发必备的环节,但在这一环节上潜藏一些坑,稍有不慎可能会造成环境配置问题。网上的方法林林总总,纷繁复杂,但其实npm官网上已经给出了最佳的安装方式:
Downloading and installing Node.js and npm
To publish and install packages to and from the public npm registry or your company’s npm Enterprise registry, you must install Node.js and the npm command line interface using either a Node version manager or a Node installer. We strongly recommend using a Node version manager to install Node.js and npm. We do not recommend using a Node installer, since the Node installation process installs npm in a directory with local permissions and can cause permissions errors when you run npm packages globally.
“我们强烈建议使用Node版本管理器来安装Node.js和npm。
我们不建议使用Node安装程序,因为Node安装过程会在具有本地权限的目录中安装npm,并且在全局运行npm软件包时可能会导致权限错误。”
后面一句话指的是在全局安装npm包时的EACCES permissions errors 错误,对于该错误,npm官网也给出解决方案:
Resolving EACCES permissions errors when installing packages globally
使用NVM安装(npm官网推荐)
- 安装NVM
$ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.34.0/install.sh | bash $ export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" $ [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm
- 检验安装效果
$ command -v nvm
出现上图所示结果即代表nvm安装成功。 - 安装node
也可以安装指定版本$ nvm install node # "node" is an alias for the latest version
$ nvm install 10.16.3
出现上图所示结果即代表node和npm安装成功。
使用n模块安装(npm官网推荐)
n – Interactively Manage Your Node.js Versions
n模块是Node.js的版本管理工具,使用它可以方便地管理多个Node版本。
操作简便,容易上手,但有一些坑。
-
如果已经有node,可以使用npm安装
$ npm install -g n
-
从github下载编译
$ make install
-
第三方安装
$ curl -L https://git.io/n-install | bash
在这种方式下需要刷新环境变量
$ source /home/hellocrease/.bashrc #我使用的账号名称为hellocrease,读者请改成自己的名字
n-install sets both PREFIX and N_PREFIX to $HOME/n, installs n to $HOME/n/bin, modifies the initialization files of supported shells to export N_PREFIX and add $HOME/n/bin to the PATH, and installs the latest LTS node version.
As a result, both n itself and all node versions it manages are hosted inside a single, optionally configurable directory, which you can later remove with the included n-uninstall script. n-update updates n itself to the latest version. See the n-install repo for more details.
使用n模块安装node和npm也会造成EACCES permissions errors (即需要使用sudo来安装npm包)
官网上给出了两种解决方法:
To avoid requiring sudo for n and npm global installs, it is suggested you either install to your home directory using N_PREFIX, or take ownership of the system directories.
- 在安装时设置N_PREFIX选项,将其安装到自定义位置
- 获得系统文件的权限
# make cache folder (if missing) and take ownership $ sudo mkdir -p /usr/local/n $ sudo chown -R $(whoami) /usr/local/n # take ownership of node install destination folders $ sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/include /usr/local/share
注:使用第三种方式安装后,如果需要删除,需要修改环境变量,将N_PREFIX那一行注释掉。
$ vi /home/hellocrease/.bashrc #我使用的账号名称为hellocrease,读者请改成自己的名字
否则会出现错误:
使用PPA方式安装
- 下载Node.js安装包
$ curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
- 安装Node.js
$ sudo apt-get install -y nodejs
- 安装Node.js版本管理模块n
$ sudo npm install -g n
- 升级Node.js
$ sudo n stable // 官方稳定版 $ sudo n latest // 官方最新版 $ sudo n lts // 官方最新LTS版本
- 查看版本号
$ npm -v $ node -v
如图,Node.js和npm已经安装成功。