Node.js Setup: NPM Installation and Path Configuration

Installing NPM

Node.js comes with the npm package manager, but it requires installation first.

  1. Download NPM: Visit https://nodejs.org to download the installer and select the version suitable for your system (Windows, Linux, or macOS).
  2. Install NPM:
    • Windows: Double-click the installer and follow the prompts to complete the installation.
    • Linux/macOS: Use the command sudo npm install -g npm to install the global version.
  3. Verify Installation: Run npm -v to confirm the NPM version; if a version number is displayed, the installation was successful.

Configuring Paths

After installation, you need to configure the path for global module installation by NPM to manage global dependencies effectively.

  • Global Installation: Use npm install -g <package> to install command-line tools; the default installation path is ~/.npm-global.
  • Modify Path: Set the global installation directory via environment variables, for example:
    export npm_config_prefix="/usr/local/lib/node_modules"
    
    This changes npm's default installation path to the specified directory, avoiding conflicts.

Common Issues

  • Permission Issues: If installation fails, try using sudo or modify permissions (e.g., chmod -R 755 ~/.npm-global).
  • Path Conflicts: If multiple versions of NPM coexist, explicitly specify the installation path using npm install -g --global-bin <package>.
  • Verify Installation: Run npm list to view installed modules and confirm whether the required tools are included.

Summary

NPM is a core tool for Node.js development; correct installation and path configuration are key steps in setting up the environment. Through installation, path setup, and verification, you can ensure an efficient and smooth development process, avoiding errors caused by path conflicts or version mismatches.