npm cheat sheet - useful npm commands
In this cheatsheet, you will find a compilation of the most common npm commands in each category that can assist you in managing your Node.js packages and projects.
Table of contents
Project initialization and management
npm init
Creates a new package.json file.
npm init
npm init --yes
Creates a new package.json file without prompting for options.
npm init --yes
Package installation and management
npm install
or npm i
Installs packages specified in the package.json file.
npm install
npm install <package-name>
or npm i <package-name>
Installs a package locally.
npm install <package-name>
npm install --save-dev
or npm i -D
Installs a package as a development package.
npm install --save-dev
npm install <package-name> --global
or npm i -g <package-name>
Installs a package globally.
npm install <package-name> --global
npm install <package-name>@<version> --save-exact
or npm i <package-name>@<version> --save-exact
Installs a package with a specific version and locks it.
npm install <package-name>@<version> --save-exact
npm install --production
or npm i --production
Installs only production packages.
npm install --production
npm uninstall <package-name>
or npm un <package-name>
This command uninstalls a locally installed package from your project. It removes the package and its dependencies from your node_modules directory and updates your package.json file's dependencies section.
npm uninstall <package-name>
npm uninstall <package-name> --global
or npm un -g <package-name>
This command uninstalls a globally installed package from your system. It removes the package and its dependencies from the global node_modules directory.
npm uninstall <package-name> --global
npm uninstall --save <package-name>
or npm un -S <package-name>
This command uninstalls a locally installed package and also removes it from your package.json file's dependencies section.
npm uninstall --save <package-name>
npm uninstall --save-dev <package-name>
or npm un -D <package-name>
This command uninstalls a locally installed package and also removes it from your package.json file's devDependencies section. This is useful when you no longer need a package for development purposes.
npm uninstall --save-dev <package-name>
npm install <package-name>@<version> --save-exact
or npm i <package-name>@<version> --save-exact
Installs a package with a specific version and locks it.
npm install <package-name> --save-exact
npm list
or npm ls
Lists all the packages installed locally.
npm list
npm list --global
or npm ls --g
Lists all the packages installed globally.
npm list --global
npm outdated
Shows outdated packages.
npm outdated
npm update
Updates packages to the latest version.
npm update
npm audit
Checks for security vulnerabilities in the installed packages.
npm audit
Global configuration
npm ls -g --depth=0
Lists only top-level packages of the global installation.
npm ls -g --depth=0
npm config list
Lists all npm configuration options.
npm config list
npm config edit
Opens the npm configuration file for editing.
npm config edit
npm cache clean
Clears the npm cache.
npm cache clean
npm whoami
Shows the username of the current user.
npm whoami
Dependency management
npm view <package-name> dependencies
Lists dependencies of a package.
npm view <package-name> dependencies
npm ls <package-name>
Shows information about a package.
npm ls <package-name>
npm ls --depth=0
Shows only top-level packages.
npm ls --depth=0
npm dedupe
Deduplicates dependencies to reduce package size.
npm dedupe
Script execution
npm run <script-name>
Runs a script defined in the package.json file.
npm run <script-name>
npm start
Runs the start script defined in the package.json file.
npm start
npm test
Runs the test script defined in the package.json file.
npm test
Package publishing and management
npm publish
Publishes a package to the npm registry.
npm publish
npm unpublish
Unpublishes a package from the npm registry.
npm unpublish
npm deprecate <package-name> <version> "<message>"
Marks a package as deprecated with a message.
npm deprecate <package-name> <version> "<message>"
npm owner add <username> <package-name>
Adds a user as a package owner.
npm owner add <username> <package-name>
npm owner rm <username> <package-name>
Removes a user as a package owner.
npm owner rm <username> <package-name>
npm search <package-name>
Searches for a package in the npm registry.
npm search <package-name>