Installing Node.js and NPM on Ubuntu
To 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 Repositories
Run the following commands to update your package list and install Node.js with NPM:sudo apt update sudo apt install nodejs npm -
Method 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 Management
NPM allows you to install, update, and manage dependencies for your Node.js projects. Below are essential commands:
-
Initialize a New Project
Create apackage.jsonfile (the manifest for your project) in your project directory:npm init -y # Uses default values; omit "-y" to customize interactively -
Install Local Packages
Install a package (e.g.,express) into your project’snode_modulesfolder and add it topackage.jsonas a dependency:npm install express # Installs the latest version npm install express@4.18.2 # Installs a specific versionUse
--save-devto add the package as a development dependency (e.g., testing tools likejest):npm install jest --save-dev -
Install Global Packages
Install packages globally (accessible from any directory) for command-line tools (e.g.,nodemon):sudo npm install -g nodemon # Requires sudo for global installations -
Update Packages
Update all installed packages to their latest versions:npm updateUpdate a specific package (e.g.,
express):npm update express -
Uninstall Packages
Remove a local package from your project andpackage.json:npm uninstall expressRemove a global package:
sudo npm uninstall -g nodemon -
Run Scripts
Execute scripts defined in yourpackage.json(e.g., astartscript to launch your app):npm run start
Configuring NPM for Better Workflow
Customize 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 registry -
Upgrade NPM to the Latest Version
Keep NPM up-to-date to access new features and security patches:npm install -g npm@latest -
Fix 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 Versions
If you need to switch between different Node.js versions (e.g., for testing compatibility), use nvm (Node Version Manager):
-
Install nvm
Run the following command to install the latest version ofnvm:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash source ~/.bashrc # Load nvm into your current shell session -
Install and Switch Node.js Versions
Install 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
以上就是关于“Ubuntu Node.js如何使用NPM包管理器”的相关介绍,筋斗云是国内较早的云主机应用的服务商,拥有10余年行业经验,提供丰富的云服务器、租用服务器等相关产品服务。云服务器资源弹性伸缩,主机vCPU、内存性能强悍、超高I/O速度、故障秒级恢复;电子化备案,提交快速,专业团队7×24小时服务支持!
简单好用、高性价比云服务器租用链接:https://www.jindouyun.cn/product/cvm