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
- Install Node.js from nodejs.org; npm comes bundled with it.
- Initialize a project with
npm initto create apackage.json. - Install packages with
npm install <package>and save dev dependencies with--save-dev. - Run scripts defined in
package.jsonusingnpm run <script>. - Update packages with
npm updateand audit security withnpm audit.
Tips
- Use
nvmorfnmto manage multiple Node.js versions. - Lock versions with
package-lock.jsonto ensure reproducible installs. - Use
npm ciin 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.