Logo

dev-resources.site

for different kinds of informations.

NPM Commands Every Web Developer Must Know

Published at
1/4/2025
Categories
webdev
npm
node
productivity
Author
dhruvdankhara
Categories
4 categories in total
webdev
open
npm
open
node
open
productivity
open
Author
13 person written this
dhruvdankhara
open
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!

npm Article's
30 articles in total
Favicon
NPM command confusion
Favicon
My First npm Package!
Favicon
Introducing date-formatter-i18n: Simplify i18n for Dates in JavaScript
Favicon
Themeify: A Simple Tool to Beautify Your React and Next.js Projects
Favicon
Mi primera Libreria en NPM
Favicon
node unsupported engine when updating npm
Favicon
Starting with semver `1.0.0`
Favicon
My Experience with Node.js Version Compatibility: Leveraging the engines Field in package.json for AutoScout
Favicon
NPM Commands Every Web Developer Must Know
Favicon
Exploring npm: The Node Package Manager
Favicon
When GitHub Actions Build Fails Due to pnpm-lockfile
Favicon
Private npm Repositories
Favicon
πŸš€ Introducing pingflow: Your Ultimate Internet Speed Testing Tool! 🌐
Favicon
npm error 'node' is not recognized as an internal or external command
Favicon
Optimer for your project security and performance issues
Favicon
Publishing NPM package with Github Actions that react-hook-use-cta used
Favicon
Building My First NPM Package: A CLI for Scaffolding Express Servers
Favicon
Resolving Peer Dependency Errors in React: A Comprehensive Guide ⚑
Favicon
Building Scalable Microservices with Node.js and Event-Driven Architecture
Favicon
NPM Dependency error
Favicon
πŸŽ„ A Christmas Gift for Developers: FileToMarkdown!
Favicon
npm
Favicon
Fastly CLI on npm: now at your JavaScript fingertips
Favicon
{ my learnings through Error message β€œerror:0308010C:digital envelope routines::unsupported” }
Favicon
Installing your react.js local package registry to your project
Favicon
External libraries: The Hidden Weight of External Libraries
Favicon
Simplifying your code: replacing heavy packages with lightweight solutions
Favicon
Lazy Devs, Rejoice! Automate Updates with Dependabot (and My Secret Sauce) πŸΉπŸ“±
Favicon
Counter - A React library for animating numeric transitions
Favicon
What I learned building vue3-search-select package

Featured ones: