准备目录
- 下载
wget https://nodejs.org/dist/v13.8.0/node-v13.8.0-linux-x64.tar.xz
更多版本见NodeJS 官网下载页 - 解压
xz -d -k node-v13.8.0-linux-x64.tar.xz
sudo tar -xvf node-v13.8.0-linux-x64.tar -C /opt
- 重命名文件夹
sudo mv /opt/node-v13.8.0-linux-x64 /opt/node13
- 查看文件夹内容
wuyujin@ubuntu18:/opt/node13$ ll
total 184
drwxr-xr-x 6 1001 1001 4096 2月 6 07:15 ./
drwxr-xr-x 14 root root 4096 2月 8 16:10 ../
drwxr-xr-x 2 1001 1001 4096 2月 6 07:15 bin/
-rw-r--r-- 1 1001 1001 53896 2月 6 07:15 CHANGELOG.md
drwxr-xr-x 3 1001 1001 4096 2月 6 07:15 include/
drwxr-xr-x 3 1001 1001 4096 2月 6 07:15 lib/
-rw-r--r-- 1 1001 1001 77130 2月 6 07:15 LICENSE
-rw-r--r-- 1 1001 1001 26508 2月 6 07:15 README.md
drwxr-xr-x 5 1001 1001 4096 2月 6 07:15 share/
wuyujin@ubuntu18:/opt/node13$ tree -L 2
.
├── bin
│ ├── node
│ ├── npm -> ../lib/node_modules/npm/bin/npm-cli.js
│ └── npx -> ../lib/node_modules/npm/bin/npx-cli.js
├── CHANGELOG.md
├── include
│ └── node
├── lib
│ └── node_modules
├── LICENSE
├── README.md
└── share
├── doc
├── man
└── systemtap
9 directories, 6 files
wuyujin@ubuntu18:/opt/node13$
配置
- 配置环境变量
目的:在任意路径下都可以调用到/opt/node13/
目录中的程序(如node
和npm
),而不用输入所调用程序的全路径。
编辑/etc/profile
,添加以下配置:
# NodeJS
export NODE_HOME=/opt/node13
export PATH=${NODE_HOME}/bin:$PATH
重启使最新配置对所有用户生效。
重启后,测试环境变量配置是否生效:
echo $NODE_HOME; echo $PATH # 查看环境变量
which node; node --version # 查看node的位置和版本
which npm; npm --version
- 设置npm仓库
目的:使包下载的速度更快。
npm是node的包管理工具(类似于python的pip)。
但是npm默认的镜像站点可能访问比较慢,可以修改为阿里云镜像。
读取值npm config get registry
设置值npm config set registry https://registry.npm.taobao.org
操作过程如下:
wuyujin@ubuntu18:/opt/node13$ # 查看npm使用的镜像站点
wuyujin@ubuntu18:/opt/node13$ npm config get registry
https://registry.npmjs.org/
wuyujin@ubuntu18:/opt/node13$
wuyujin@ubuntu18:/opt/node13$ # 设置为阿里云镜像站点
wuyujin@ubuntu18:/opt/node13$ npm config set registry https://registry.npm.taobao.org
wuyujin@ubuntu18:/opt/node13$
wuyujin@ubuntu18:/opt/node13$ # 重新读取,看是否设置成功那个
wuyujin@ubuntu18:/opt/node13$ npm config get registry
https://registry.npm.taobao.org/
wuyujin@ubuntu18:/opt/node13$
运行
wuyujin@ubuntu18:/opt/node13$ node
Welcome to Node.js v13.8.0.
Type ".help" for more information.
> console.log("你好 wuyujin");
你好 wuyujin
undefined
> .exit
wuyujin@ubuntu18:/opt/node13$
命令行安装
除了使用压缩包安装,也可以直接使用Linux系统的包管理工具,快捷方便(需要联网)。
- 安装
sudo apt-get install nodejs
- 卸载
sudo apt-get remove nodejs
- 查询安装位置
dpkg -L nodejs