How to Use npm for Package Management: Essential Commands and Workflows

npm (Node Package Manager) is the default package manager for Node.js, providing access to hundreds of thousands of open-source libraries. Mastering npm is essential for installing dependencies, managing versions, and automating project tasks. This guide covers the core workflows you need to manage packages effectively.

Before using npm, ensure Node.js is installed. Verify with node -v and npm -v in your terminal. npm ships with Node.js, so no separate installation is required.

Article illustration

Initializing a Project

Navigate to your project folder and run npm init. This creates a package.json file—the heart of your project. Use npm init -y to accept default values quickly. The package.json stores metadata, dependencies, and scripts.

Installing Packages

To install a package as a production dependency, run npm install <package-name>. This adds the package to the dependencies field and creates a node_modules folder. For development tools like linters or test runners, use npm install --save-dev to register them under devDependencies.

Useful variations:

  • npm install -g for global packages like CLI tools
  • npm install alone installs all dependencies listed in package.json
  • npm install --save-exact to lock exact versions

Updating and Removing Packages

Run npm update to update packages within your specified version ranges. Use npm outdated to check for newer versions. To remove a package, run npm uninstall <package-name>; add --save-dev if it was a dev dependency.

Running Scripts

The scripts section in package.json lets you automate tasks. Define custom commands under “scripts”, then execute them with npm run <script-name>. For example, npm start runs the “start” script.

Learning these npm fundamentals will speed up your development and keep projects organized. Start small, practice with common packages, and consult npm help when in doubt.

sarah antaboga
Author: sarah antaboga

Leave a Reply

Your email address will not be published. Required fields are marked *