在 Linux 上搭建 Node.js 开发环境可以通过以下步骤进行:
Ubuntu/Debian:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
CentOS/RHEL:
curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo -E bash -
sudo yum install -y nodejs
Ubuntu/Debian:
sudo apt-get update
sudo apt-get install nodejs npm
CentOS/RHEL:
sudo yum install -y nodejs npm --enablerepo epel
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install node
或者安装特定版本:nvm install 16.14.0
npm install -g npm
mkdir mynodeapp
cd mynodeapp
npm init -y
npm install express
server.js
文件并编写代码:const express = require('express');
const app = express();
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
node server.js
http://localhost:3000
。sudo apt install git
通过以上步骤,你可以在 Linux 上成功搭建 Node.js 开发环境。如果遇到问题,可以参考 Node.js 官方文档或社区论坛获取帮助。
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: arch linux如何配置图形界面