在 Ubuntu/Debian 上安装最新的 Node.js 和 NPM
欢迎阅读我们有关如何在 Ubuntu 和 Debian Linux 发行版上安装最新 Node.js 和 NPM 的指南。 Node.js 是一种免费开源的服务器端编程语言,可以在各种平台(Linux、Windows、Unix、macOS)上运行。 Node.js 是一个基于 Chrome V8 引擎构建的 JavaScript 运行时,用于构建快速且可扩展的网络应用程序。
第 1 步:添加 Node.js APT 存储库
最新的 Node.js 包可在 APT 存储库中获取。首先,更新您的系统并安装一些依赖项。
sudo apt update
sudo apt install curl dirmngr apt-transport-https lsb-release ca-certificates vim
如果您想了解最新版本,请查看 Node.js 版本页面
添加 Node.js LTS 存储库
如果您想使用 LTS 版本,请为该版本添加 APT。例如,对于 Node.js 16.x
。它将像这样添加(偶数);
### Node.js 18 LTS ###
curl -sL https://deb.nodesource.com/setup_18.x | sudo -E bash -
### Node.js 16 LTS ###
curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -
### Node.js 14 LTS ###
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
步骤 2:在 Ubuntu/Debian 上安装最新的 Node.js
添加存储库后,继续安装 Node.js 和 NPM。
sudo apt install nodejs
您还可以开发工具来构建本机插件:
sudo apt install gcc g++ make
确认版本。
$ node --version
v18.2.0
$ npm --version
8.9.0
第 3 步:安装 Yarn 包管理器(额外和可选)
如果您需要纱线包管理器,请通过运行以下命令来安装它:
curl -sL https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
sudo apt update && sudo apt install yarn
确认安装。
$ yarn --version
1.22.19
第 4 步:测试 Node.js
让我们创建一个简单的 Nodejs 应用程序来测试 Node.js 是否正常工作。
mkdir /tmp/node-demo
cd /tmp/node-demo
使用默认的 package.json 文件初始化nodejs项目:
$ npm init -y
Wrote to /tmp/node-demo/package.json:
{
"name": "node-demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC"
}
创建一个 index.js
vim index.js
添加 :
const express = require('express');
const PORT = 8080;
const HOST = '0.0.0.0';
const app = express();
app.get('/', (req, res) => {
res.send('Hello Node.js World\n');
});
app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);
使用 npm 命令安装 Express 包:
$ npm install -save express
added 57 packages, and audited 58 packages in 2s
7 packages are looking for funding
run `npm fund` for details
found 0 vulnerabilities
npm notice
npm notice New minor version of npm available! 8.9.0 -> 8.11.0
npm notice Changelog: https://github.com/npm/cli/releases/tag/v8.11.0
npm notice Run npm install -g [email to update!
npm notice
将启动脚本添加到我们的 package.json
文件中
$ vim package.json
{
"name": "demo",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"start": "node index.js"
},
"keywords": [],
"author": "",
"license": "ISC",
"dependencies": {
"express": "^4.18.1"
}
}
您现在可以运行 Nodejs 应用程序:
$ node index.js
Running on http://0.0.0.0:8080
##OR
$ npm start
Running on http://0.0.0.0:8080
可以使用以下命令在启用调试的情况下启动应用程序:
$ node --inspect index.js
Debugger listening on ws://127.0.0.1:9229/49cd62a8-88e0-4b21-b898-f79a79e9d5dc
For help, see: https://nodejs.org/en/docs/inspector
Running on http://0.0.0.0:8080
如果您通过端口 8080
访问服务器 IP,您应该会看到 Node.js 应用程序输出。
就这样吧! Node.js 已成功安装在 Ubuntu/Debian Linux 发行版上。祝节点开发愉快。
有关的 :
- 在 RHEL/CentOS 8 上安装 PM2 Node.js 进程管理器
- 如何在 RHEL 8 上安装 Node.js 12,10 LTS