Linux上搭建JS开发环境
一 安装 Node.js 与包管理器
sudo apt update
sudo apt install -y nodejs npm
sudo yum install -y epel-release
sudo yum install -y nodejs npm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install --lts # 或 nvm install node
nvm use --lts
# Ubuntu/Debian 示例(按需替换版本号)
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
node -v
npm -v
以上方法覆盖系统包管理器、NVM 与 NodeSource 三种常见方式,适合不同版本与灵活性需求。
二 选择与安装编辑器
sudo apt update
sudo apt install -y software-properties-common apt-transport-https wget
wget -q https://packages.microsoft.com/keys/microsoft.asc -O- | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://packages.microsoft.com/repos/vscode stable main"
sudo apt update
sudo apt install -y code
code --install-extension dbaeumer.vscode-eslint
code --install-extension esbenp.prettier-vscode
code --install-extension msjsdiag.debugger-for-chrome
VS Code 提供完善的 JS/TS 生态与调试体验,配合 ESLint、Prettier 可显著提升代码质量与一致性。
三 创建项目与运行调试
mkdir my-js-project && cd $_
npm init -y
npm i -D webpack webpack-cli babel-loader @babel/core @babel/preset-env
npm i -D webpack-dev-server
# package.json
{
"scripts": { "start": "webpack serve --open" }
}
npm start
node index.js
node inspect app.js
{
"version": "0.2.0",
"configurations": [
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"program": "${workspaceFolder}/index.js"
}
]
}
以上覆盖了从初始化、依赖管理、开发服务器到断点调试的完整链路。
四 前端项目与浏览器运行
<!doctype html>
<meta charset="utf-8">
<title>JS in Browser</title>
<script src="index.js"></script>
npm i -D webpack-dev-server
五 进阶与常用工具
npm i -g pm2
pm2 start app.js --name "my-app"
pm2 logs
pm2 monit
curl -fsSL https://deno.land/x/install/install.sh | sh
deno run deno_app.js
curl -o- -L https://yarnpkg.com/install.sh | bash
yarn init -y
sudo apt install -y git
git init
git add .
git commit -m "Initial commit"
以上工具覆盖进程守护、替代运行时与团队协作基础能力。