总目录 - Ubuntu 应用软件安装 && 开发环境搭建测试 目录
使用包管理器直接命令行安装
- 查询要安装的软件包名称
apt-cache search nodejs
wuyujin@ubuntu18:~$ sudo apt-cache search nodejs | grep ^nodejs
nodejs - evented I/O for V8 javascript
nodejs-dev - evented I/O for V8 javascript (development files)
nodejs-doc - API documentation for Node.js, the javascript platform
nodejs-mozilla - evented I/O for V8 javascript
wuyujin@ubuntu18:~$
- 安装
sudo apt-get install nodejs
- 卸载
sudo apt-get remove nodejs --purge
- 查询安装位置
dpkg -L nodejs
- 查询启动程序的位置
which node
如果机器没有网络,或其他原因安装失败。也可以尝试以下步骤,使用压缩包解压安装。
手动下载安装
准备目录
- 下载
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 --version
v13.8.0
wuyujin@ubuntu18:/opt/node13$ node
Welcome to Node.js v13.8.0.
Type ".help" for more information.
> console.log("你好 wuyujin");
你好 wuyujin
undefined
> .help
.break Sometimes you get stuck, this gets you out
.clear Alias for .break
.editor Enter editor mode
.exit Exit the repl
.help Print this help message
.load Load JS from a file into the REPL session
.save Save all evaluated commands in this REPL session to a file
Press ^C to abort current expression, ^D to exit the repl
> .exit
wuyujin@ubuntu18:/opt/node13$