在 Ubuntu 中配置 Node.js 環境,你需要遵循以下步驟:
sudo apt update
sudo apt install nodejs
sudo apt install npm
這將同時安裝 Node.js 和 npm(Node.js 的包管理器)。
node --version
npm --version
如果看到 Node.js 和 npm 的版本號,說明安裝成功。
打開 ~/.bashrc
文件(或者 ~/.zshrc
,如果你使用的是 Zsh),在文件末尾添加以下內容:
export PATH=$PATH:/usr/bin/node
export PATH=$PATH:/usr/bin/npm
保存文件并關閉編輯器。
source ~/.bashrc
或者,如果你使用的是 Zsh:
source ~/.zshrc
現在,你應該可以在任何位置運行 Node.js 和 npm 命令了。
/usr/local/lib/node_modules
目錄中。如果你想更改全局模塊的安裝路徑,可以按照以下步驟操作:mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
~/.profile
或 ~/.bashrc
文件,并添加以下行:export PATH=~/.npm-global/bin:$PATH
source ~/.profile
或 source ~/.bashrc
現在,當你使用 npm install -g
命令安裝全局模塊時,它們將被安裝在 ~/.npm-global/lib/node_modules
目錄中。