在 Linux 上配置 Node.js 主要包括以下几个步骤:
sudo apt update
sudo apt install nodejs npm
sudo yum install nodejs npm
或者,对于较新的 CentOS 版本,使用 dnf:
sudo dnf install nodejs npm
NodeSource 提供了最新版本的 Node.js。以下是安装步骤:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
sudo apt-get install -y nodejs
NVM 是一个 Node.js 版本管理器,可以方便地安装和管理不同版本的 Node.js。
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc
nvm install node
无论使用哪种方法安装,都可以通过以下命令验证 Node.js 和 npm 是否安装成功:
node -v
npm -v
mkdir myproject
cd myproject
npm init -y
npm install --save-dev npm-check nodemon eslint prettier husky
选择合适的编辑器或 IDE(如 Visual Studio Code、WebStorm 等),并安装必要的插件。
在项目根目录下创建一个 .env
文件来存储环境变量,并使用 dotenv 等包来加载这些变量。
初始化 Git 仓库:
git init
配置 Git 忽略文件:
touch .gitignore
提交首次更改:
git add .
git commit -m "Initial commit"
以上步骤涵盖了在 Linux 上安装和配置 Node.js 的基本流程。根据具体需求,可能还需要进行其他配置,如设置 Node.js 的版本、配置构建工具等。