npm cheat sheet: useful npm commands

A quick reference for creating projects, managing dependencies, running scripts, and publishing packages with npm.


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 <package-name>

or npm i -D <package-name>

Installs a package and adds it to devDependencies.

npm install --save-dev <package-name>

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 specific version and saves it without a version range.

npm install <package-name>@<version> --save-exact

npm install --omit=dev

Installs the project while omitting devDependencies from disk.

npm install --omit=dev


npm uninstall <package-name>

or npm un <package-name>

Removes a package from node_modules and updates package.json and package-lock.json.

npm uninstall <package-name>

npm uninstall <package-name> --global

or npm un -g <package-name>

Removes a globally installed package.

npm uninstall <package-name> --global


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 installed packages to the newest versions allowed by their configured version ranges.

npm update


npm audit

Reports known vulnerabilities in the project's dependency tree.

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 active npm configuration values.

npm config list

npm config edit

Opens the npm configuration file for editing.

npm config edit


npm cache verify

Verifies the integrity of the npm cache and removes unneeded data.

npm cache verify


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 where a package appears in the current dependency tree.

npm ls <package-name>

npm ls --depth=0

Shows only top-level packages.

npm ls --depth=0


npm dedupe

Moves compatible dependencies higher in the tree to reduce duplication.

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 <package-name>@<version>

Removes a published package version, subject to the registry's unpublish policy.

npm unpublish <package-name>@<version>


npm deprecate <package-name>@<version> "<message>"

Marks a package version or version range as deprecated and adds 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>


Searches for a package in the npm registry.

npm search <package-name>