Node.js Setup: NPM Installation and Path Configuration
Installing NPM
Node.js comes with the npm package manager, but it requires installation first.
- Download NPM: Visit https://nodejs.org to download the installer and select the version suitable for your system (Windows, Linux, or macOS).
- Install NPM:
- Windows: Double-click the installer and follow the prompts to complete the installation.
- Linux/macOS: Use the command
sudo npm install -g npmto install the global version.
- Verify Installation: Run
npm -vto 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:
This changes npm's default installation path to the specified directory, avoiding conflicts.export npm_config_prefix="/usr/local/lib/node_modules"
Common Issues
- Permission Issues: If installation fails, try using
sudoor 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 listto 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.