温馨提示×

Debian上Node.js的模块安装与管理

小樊
56
2025-10-08 03:52:44
栏目: 编程语言

Installing Node.js and npm on Debian
Before managing Node.js modules, you need to install Node.js and its default package manager, npm. On Debian, the easiest way is to use the official NodeSource repository for the latest stable versions:

  1. Update your system’s package list: sudo apt update.
  2. Add the NodeSource repository (replace lts with a specific version like 16.x if needed):
    curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
    
  3. Install Node.js and npm: sudo apt install -y nodejs.
  4. Verify installation:
    node -v  # Checks Node.js version
    npm -v   # Checks npm version
    

This ensures you have the latest Node.js and npm versions compatible with Debian.

Using npm for Module Management
npm (Node Package Manager) is the default tool for managing Node.js modules. Below are core operations:

  • Initialize a Project: Create a package.json file to track dependencies. Navigate to your project directory and run:
    npm init -y  # Auto-generates package.json with default settings
    
  • Install Modules:
    • Local Installation (project-specific, saves to node_modules and package.json):
      npm install express --save  # --save is optional (default in npm 5+)
      
    • Global Installation (system-wide, for CLI tools like nodemon):
      sudo npm install -g nodemon  # Requires sudo for global access
      
  • Update Modules: Update all local modules to their latest versions:
    npm update
    
    To update a specific module (e.g., express):
    npm update express
    
  • Uninstall Modules: Remove a local module and its entry from package.json:
    npm uninstall express
    
    For global modules:
    sudo npm uninstall -g nodemon
    
  • View Installed Modules: List all local modules:
    npm list --depth=0  # Shows top-level dependencies only
    
    For global modules:
    npm list -g --depth=0
    

These commands help manage dependencies efficiently and keep your project organized.

Using Yarn for Module Management
Yarn is an alternative to npm, offering faster installations and deterministic dependency resolution. To use Yarn on Debian:

  • Install Yarn: First, ensure npm is installed, then run:
    sudo npm install -g yarn
    
  • Initialize a Project: Create a package.json file (similar to npm):
    yarn init -y
    
  • Install Modules:
    • Local Installation (saves to package.json and generates a yarn.lock file for version locking):
      yarn add express
      
    • Global Installation (for CLI tools):
      yarn global add nodemon
      
  • Update Modules: Update all local modules to their latest versions:
    yarn upgrade
    
    To update a specific module (e.g., express):
    yarn upgrade express
    
  • Uninstall Modules: Remove a local module and update package.json/yarn.lock:
    yarn remove express
    
    For global modules:
    yarn global remove nodemon
    
  • View Installed Modules: List all local modules:
    yarn list --depth=0
    
    For global modules:
    yarn list -g --depth=0
    

Yarn’s yarn.lock file ensures consistent dependency versions across environments, making it a popular choice for team projects.

Managing Node.js Versions with nvm
If you need to switch between multiple Node.js versions (e.g., for testing compatibility), use nvm (Node Version Manager):

  • Install nvm: Run the following command to install the latest version of nvm:
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    
    Reload your terminal or run source ~/.nvm/nvm.sh to apply changes.
  • Install Node.js Versions: Install a specific version (e.g., 16.13.0) or the latest LTS version:
    nvm install 16.13.0  # Specific version
    nvm install --lts    # Latest LTS version
    
  • Switch Versions: Use nvm use to activate a version for the current session:
    nvm use 16.13.0
    
  • Set Default Version: Make a version the default for new terminals:
    nvm alias default 16.13.0
    
  • View Installed Versions: List all installed versions:
    nvm ls
    

nvm is essential for developers working on projects that require different Node.js versions, ensuring compatibility without conflicts.

0