DEV Community

Cover image for NPM Commands Every Web Developer Must Know
Dhruv Dankhara
Dhruv Dankhara

Posted on

NPM Commands Every Web Developer Must Know

Node Package Manager (NPM) is a crucial tool for web developers, helping manage dependencies, automate tasks, and streamline project workflows. Whether you're a beginner or an experienced developer, mastering essential NPM commands can boost your efficiency. In this guide, we'll explore the most useful NPM commands every web developer should know.


Table of Contents

1. Installing and Managing Packages

1.1 Install a Package Locally

1.2 Install a Package Globally

1.3 Install Dev Dependencies

2. Managing Packages

2.1 Update Packages

2.2 Check for Outdated Packages

2.3 Upgrade to Latest Version

3. Removing and Cleaning Dependencies

3.1 Uninstall a Package

3.2 Remove Unused Packages

4. Running Scripts

4.1 Run Custom Scripts

4.2 Start a Project

5. Initializing and Managing Projects

5.1 Initialize a Project

5.2 View Installed Packages

### 6. Managing Cache and Debugging

6.1 Clear Cache

6.2 Audit for Security Issues

7. Conclusion


1. Installing and Managing Packages

1.1 Install a Package Locally

npm install package-name
Enter fullscreen mode Exit fullscreen mode

or

npm i package-name
Enter fullscreen mode Exit fullscreen mode

Installs a package in the node_modules folder and adds it to package.json under dependencies.

Example:

npm install express
Enter fullscreen mode Exit fullscreen mode

Installs Express.js for building web applications.

1.2 Install a Package Globally

npm install -g package-name
Enter fullscreen mode Exit fullscreen mode

Global installation makes the package accessible across all projects.

Example:

npm install -g nodemon
Enter fullscreen mode Exit fullscreen mode

Installs Nodemon, a tool that automatically restarts the server on file changes.

1.3 Install Dev Dependencies

npm install package-name --save-dev
Enter fullscreen mode Exit fullscreen mode

or

npm i package-name -D
Enter fullscreen mode Exit fullscreen mode

Installs packages needed for development but not in production.

Example:

npm install eslint --save-dev
Enter fullscreen mode Exit fullscreen mode

Installs ESLint for code linting.

2. Managing Packages

2.1 Update Packages

npm update package-name
Enter fullscreen mode Exit fullscreen mode

Updates a specific package to the latest compatible version.

Example:

npm update react
Enter fullscreen mode Exit fullscreen mode

Updates React to the latest version allowed by package.json.

2.2 Check for Outdated Packages

npm outdated
Enter fullscreen mode Exit fullscreen mode

Lists all outdated packages in your project.

2.3 Upgrade to Latest Version

npm upgrade
Enter fullscreen mode Exit fullscreen mode

or

npm install package-name@latest
Enter fullscreen mode Exit fullscreen mode

Upgrades all dependencies or a specific package to the latest version.

Example:

npm install lodash@latest
Enter fullscreen mode Exit fullscreen mode

Upgrades Lodash to the latest release.

3. Removing and Cleaning Dependencies

3.1 Uninstall a Package

npm uninstall package-name
Enter fullscreen mode Exit fullscreen mode

or

npm remove package-name
Enter fullscreen mode Exit fullscreen mode

Removes a package from node_modules and package.json.

Example:

npm uninstall moment
Enter fullscreen mode Exit fullscreen mode

Removes Moment.js from the project.

3.2 Remove Unused Packages

npm prune
Enter fullscreen mode Exit fullscreen mode

Deletes unnecessary packages not listed in package.json.

4. Running Scripts

4.1 Run Custom Scripts

npm run script-name
Enter fullscreen mode Exit fullscreen mode

Executes a script defined in package.json.

Example (package.json):

"scripts": {
  "start": "node index.js",
  "dev": "nodemon index.js"
}
Enter fullscreen mode Exit fullscreen mode

To run the development script:

npm run dev
Enter fullscreen mode Exit fullscreen mode

4.2 Start a Project

npm start
Enter fullscreen mode Exit fullscreen mode

Runs the start script from package.json.

5. Initializing and Managing Projects

5.1 Initialize a Project

npm init
Enter fullscreen mode Exit fullscreen mode

Starts an interactive setup to create a package.json file.

For a quicker setup without prompts:

npm init -y
Enter fullscreen mode Exit fullscreen mode

Creates package.json with default values.

5.2 View Installed Packages

npm list
Enter fullscreen mode Exit fullscreen mode

Shows installed dependencies in the project.

For globally installed packages:

npm list -g --depth=0
Enter fullscreen mode Exit fullscreen mode

6. Managing Cache and Debugging

6.1 Clear Cache

npm cache clean --force
Enter fullscreen mode Exit fullscreen mode

Clears the NPM cache to fix installation issues.

6.2 Audit for Security Issues

npm audit
Enter fullscreen mode Exit fullscreen mode

Scans dependencies for vulnerabilities.

To fix issues automatically:

npm audit fix
Enter fullscreen mode Exit fullscreen mode

Conclusion

Mastering NPM commands is essential for managing packages efficiently in web development. Whether you're installing dependencies, running scripts, or debugging issues, these commands streamline your workflow. Bookmark this guide and improve your development experience!

Top comments (0)