Ubuntu Node.js如何使用NPM包管理器

作者:袖梨 2026-08-07

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).

Ubuntu Node.js如何使用NPM包管理器

  1. 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 npm
  2. 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 ManagementNPM allows you to install, update, and manage dependencies for your Node.js projects. Below are essential commands:

  1. Initialize a New ProjectCreate a package.json file (the manifest for your project) in your project directory:

    npm init -y# Uses default values; omit "-y" to customize interactively
  2. Install Local PackagesInstall a package (e.g., express) into your project’s node_modules folder and add it to package.json as a dependency:

    npm install express# Installs the latest versionnpm install [email protected] # Installs a specific version

    Use --save-dev to add the package as a development dependency (e.g., testing tools like jest):

    npm install jest --save-dev
  3. Install Global PackagesInstall packages globally (accessible from any directory) for command-line tools (e.g., nodemon):

    sudo npm install -g nodemon# Requires sudo for global installations
  4. Update PackagesUpdate all installed packages to their latest versions:

    npm update

    Update a specific package (e.g., express):

    npm update express
  5. Uninstall PackagesRemove a local package from your project and package.json:

    npm uninstall express

    Remove a global package:

    sudo npm uninstall -g nodemon
  6. Run ScriptsExecute scripts defined in your package.json (e.g., a start script to launch your app):

    npm run start

Configuring NPM for Better WorkflowCustomize NPM’s behavior to suit your needs:

  1. 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.com

    Verify the change:

    npm config get registry
  2. Upgrade NPM to the Latest VersionKeep NPM up-to-date to access new features and security patches:

    npm install -g npm@latest
  3. Fix Global Installation Permissions (Avoid sudo)By default, global installations require sudo, which can cause permission issues. To fix this, change the ownership of the global node_modules folder:

    sudo chown -R $USER /usr/local/lib/node_modules

    After 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):

  1. 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 session
  2. Install and Switch Node.js VersionsInstall a specific version of Node.js (e.g., 14.17.0):

    nvm install 14.17.0

    Switch to the installed version:

    nvm use 14.17.0

    Set a default version (used when opening new terminals):

    nvm alias default 14.17.0

相关文章

精彩推荐