How to Use npm and Node.js Effectively

· Category: VS Code & Developer Tools

Short answer

npm is the default package manager for Node.js. It installs dependencies, runs scripts, and publishes packages defined in package.json.

Steps

  1. Install Node.js from nodejs.org; npm comes bundled with it.
  2. Initialize a project with npm init to create a package.json.
  3. Install packages with npm install <package> and save dev dependencies with --save-dev.
  4. Run scripts defined in package.json using npm run <script>.
  5. Update packages with npm update and audit security with npm audit.

Tips

  • Use nvm or fnm to manage multiple Node.js versions.
  • Lock versions with package-lock.json to ensure reproducible installs.
  • Use npm ci in CI/CD for faster, reliable installs.

Common issues

  • Permission errors during global installs can be fixed by configuring a custom prefix.
  • Conflicting peer dependencies may require --legacy-peer-deps.