0%

【Nodejs】安装 Nodejs

安装 nvm

下载安装脚本 | 运行

1
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.2/install.sh | bash

添加暂时的环境变量

这将在登出 shell 后失效

1
2
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

添加永久的环境变量

这意味着每次登录 shell 后,会自动加载这些环境变量

1
2
echo export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" >> /etc/profile
echo "[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"" >> /etc/profile

安装 nodejs

1
2
3
4
5
6
7
8
9
10
11
12
# 列出所有版本
nvm list-remote
# 安装最新长期支持版
nvm install v12.16.1
# 切换版本
nvm use v12.16.1
# 设置默认版本
nvm alias default v12.16.1

# 查看版本
node -v
npm -v

更改镜像源

默认镜像源太慢,改用淘宝镜像源

1
2
3
4
5
6
7
# 查看镜像源
npm config get registry
# 使用淘宝镜像源
npm config set registry https://registry.npm.taobao.org
npm config set sass_binary_site=https://npm.taobao.org/mirrors/node-sass/
npm config set phantomjs_cdnurl=https://npm.taobao.org/mirrors/phantomjs/
npm config set electron_mirror=https://npm.taobao.org/mirrors/electron/

2020/3/13 更新

淘宝源的 electron 镜像有坑:

1
(node:11316) UnhandledPromiseRejectionWarning: HTTPError: Response code 404 (Not Found) for https://npm.taobao.org/mirrors/electron/v8.1.1/electron-v8.1.1-win32-x64.zip

去淘宝镜像源网站看了一下,地址是这个:

1
https://npm.taobao.org/mirrors/electron/8.1.1/electron-v8.1.1-win32-x64.zip

就少了一个 v

需要单独设置 electron 路径了:

1
npm config set electron_custom_dir "8.1.1"

参考:https://www.cnblogs.com/ygjzs/p/12457945.html

淘宝镜像源 electron

npm 配置文件

  • 配置文件中有镜像源设置,可以自行更改
1
nano ~/.npmrc