最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
Ubuntu Node.js如何使用NPM包管理器
时间:2026-08-07 14:38:50 编辑:袖梨 来源:一聚教程网
Installing Node.js and NPM on UbuntuTo use NPM, you first need to install Node.js (which includes NPM by default). There are two common methods: using Ubuntu’s official repositories or the NodeSource repository (recommended for the latest stable version).

Method 1: Install from Ubuntu’s Official RepositoriesRun the following commands to update your package list and install Node.js with NPM:
sudo apt updatesudo apt install nodejs npmMethod 2: Install from NodeSource Repository (Recommended)For the latest stable version of Node.js, add the NodeSource repository and install:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -# Replace "16.x" with your desired version (e.g., 18.x)sudo apt install -y nodejs
After installation, verify success by checking the versions:
node -v# Displays Node.js version (e.g., v16.14.0)npm -v # Displays NPM version (e.g., 8.5.0)Basic NPM Commands for Package ManagementNPM allows you to install, update, and manage dependencies for your Node.js projects. Below are essential commands:
Initialize a New ProjectCreate a
package.jsonfile (the manifest for your project) in your project directory:npm init -y# Uses default values; omit "-y" to customize interactivelyInstall Local PackagesInstall a package (e.g.,
express) into your project’snode_modulesfolder and add it topackage.jsonas a dependency:npm install express# Installs the latest versionnpm install [email protected] # Installs a specific versionUse
--save-devto add the package as a development dependency (e.g., testing tools likejest):npm install jest --save-devInstall Global PackagesInstall packages globally (accessible from any directory) for command-line tools (e.g.,
nodemon):sudo npm install -g nodemon# Requires sudo for global installationsUpdate PackagesUpdate all installed packages to their latest versions:
npm updateUpdate a specific package (e.g.,
express):npm update expressUninstall PackagesRemove a local package from your project and
package.json:npm uninstall expressRemove a global package:
sudo npm uninstall -g nodemonRun ScriptsExecute scripts defined in your
package.json(e.g., astartscript to launch your app):npm run start
Configuring NPM for Better WorkflowCustomize NPM’s behavior to suit your needs:
Set a Mirror Registry (Accelerate Downloads)Switch to a faster mirror (e.g., Taobao) to avoid slow downloads from the official registry:
npm config set registry https://registry.npmmirror.comVerify the change:
npm config get registryUpgrade NPM to the Latest VersionKeep NPM up-to-date to access new features and security patches:
npm install -g npm@latestFix Global Installation Permissions (Avoid
sudo)By default, global installations requiresudo, which can cause permission issues. To fix this, change the ownership of the globalnode_modulesfolder:sudo chown -R $USER /usr/local/lib/node_modulesAfter running this command, you can install global packages without
sudo.
Optional: Use nvm to Manage Multiple Node.js VersionsIf you need to switch between different Node.js versions (e.g., for testing compatibility), use nvm (Node Version Manager):
Install nvmRun the following command to install the latest version of
nvm:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bashsource ~/.bashrc# Load nvm into your current shell sessionInstall and Switch Node.js VersionsInstall a specific version of Node.js (e.g., 14.17.0):
nvm install 14.17.0Switch to the installed version:
nvm use 14.17.0Set a default version (used when opening new terminals):
nvm alias default 14.17.0
相关文章
- 明末渊虚之羽刘承恩怎么打 08-07
- 神庙逃亡2网页版-神庙逃亡2在线游戏入口 08-07
- 植物大战僵尸杂交重制中气球扶梯僵尸如何 08-07
- 美图秀秀网页版入口-美图秀秀在线使用 08-07
- 炉石传说无穷之手卡牌图鉴 08-07
- DeepSeek入口网页版 08-07