npm commands cheat sheet

This cheat sheet brings together the most useful commands of npm, the Node.js package manager: installing, updating, and removing packages, installing for production with npm ci, scripts and npx, checking for security vulnerabilities, settings, and version ranges. You can search for a command, filter by topic, and copy it with one click.

Versions and help

  • node -v

    Shows the version of Node.js installed on the server. The full form is node --version.

  • npm -v

    Shows the npm version. The full form is npm --version.

  • npm help

    Shows the list of npm commands.

  • npm install -h

    Shows a usage summary for a specific command, in this example install.

New projects and package.json

  • npm init

    Creates a new package.json file for the project, by asking about the name, version, description, and entry point. When the file already exists, the fields defined in it are kept.

  • npm init -y

    Creates package.json with the default values, without asking questions. -y is short for --yes.

  • npm pkg get scripts.test

    Reads a field from package.json, in this example the test script. Without a field name, the whole file is shown.

  • npm pkg set engines.node=">=22"

    Writes a field in package.json without editing the file by hand, in this example the Node.js versions the project supports. npm pkg delete deletes a field.

  • "engines": { "node": ">=22" }

    The engines field declares which Node.js versions the project works with. By default, it's only a recommendation that shows a warning when the package is installed as a dependency. The engine-strict=true setting makes npm refuse to install packages that declare they're not compatible with the current Node.js version.

  • "private": true

    A line in package.json that makes npm refuse to publish the package to the public registry. Recommended for every application or website that isn't meant to be published as a package.

Installing packages

  • npm install

    Installs all the project dependencies from package.json into the node_modules directory. When the versions in the package-lock.json file match the ranges in package.json, exactly the versions in the lock file are installed. Otherwise, npm resolves new versions and updates the lock file. The short form is npm i.

  • npm install express

    Installs a package, adds it to the dependencies list in package.json, and updates package-lock.json. By default, the range is saved with the ^ sign.

  • npm install lodash@4.17.21

    Installs a specific version of a package. The installation fails when that version wasn't published to the registry. Instead of a version number, you can give a tag, for example lodash@latest for the version tagged latest.

  • npm install -D eslint

    Adds a package to the devDependencies list, which is meant for tools needed only for development and testing. When you install for production with --omit=dev, these packages aren't installed. -D is short for --save-dev.

  • npm install -E express

    Saves an exact version in package.json, without the ^ sign. -E is short for --save-exact.

  • npm install --no-save express

    Installs a package into the node_modules directory without adding it to package.json.

  • npm install --ignore-scripts

    Installs without running scripts from package.json. Suitable for a project from an unknown source, because scripts get access to the account of the user who runs npm. Packages that need a build step during installation may not work.

Global packages

  • npm install -g pm2

    Installs a package globally, so its commands are available from any directory and not only inside a specific project. The packages are stored in the prefix directory, which by default is the directory where Node.js is installed. When that's a system directory, the installation fails with an EACCES error for a regular user. -g is short for --global.

  • npm config set prefix ~/.local

    Moves global installations to a directory in the user's home directory, so there's no need for sudo. After this setting, add the line PATH=~/.local/bin:$PATH to the ~/.profile file and run source ~/.profile. Another way the npm documentation recommends is to install Node.js with a version manager.

  • npm ls -g

    Lists the installed global packages.

  • npm root -g

    Shows the node_modules directory where global packages are stored. Without -g, it shows the node_modules directory of the current project.

  • npm update -g

    Updates global packages to the version tagged latest in the registry. A package you upgraded to a version newer than latest, such as a beta, is downgraded to latest.

  • npm uninstall -g pm2

    Removes a global package.

npm ci and production installs

  • npm ci

    A clean install from package-lock.json, suitable for a production server and for automated tests (CI). The command requires a lock file, deletes the existing node_modules directory before installing, and never changes package.json or the lock file. When the lock file doesn't match package.json, it stops with an error.

  • npm ci --omit=dev

    Installs for a production server without the packages in the devDependencies list. The omitted packages are still listed in package-lock.json, they just aren't installed on disk.

  • npm install --omit=dev

    Installs without the development packages, when there's no lock file or when the lock file also needs updating. When the NODE_ENV environment variable is set to production, npm omits the development packages even without this option. The old --production option is deprecated, and you use the --omit=dev option instead.

  • npm prune --omit=dev

    Removes the development packages from node_modules, along with packages that aren't listed as a dependency of any package in the project. The --dry-run option shows what would be removed, without actually changing anything.

  • npm install-scripts ls

    In recent versions of npm, install scripts of dependencies are blocked by default, and npm shows which packages were skipped. This command lists the packages whose scripts haven't been approved yet, without changing any files.

  • npm install-scripts approve sharp

    Allows a package to run install scripts, for example a package that builds a native component during installation. The approval is saved in the allowScripts field in package.json and, by default, applies only to the version you checked. The short alias is npm approve-scripts sharp.

  • npm install-scripts deny sharp

    Records in allowScripts that the package's scripts won't run. The denial stays in place even when you approve all packages at once. The short alias is npm deny-scripts sharp.

Updating and removing packages

  • npm outdated

    Shows packages that have a newer version. The Wanted column is the highest version that matches the range in package.json, and the Latest column is the version tagged latest in the registry. Red: a compatible version exists, and it's recommended to update. Yellow: the new version is outside the range, usually a new major version, and the update needs testing.

  • npm update

    Updates the packages to the latest version that matches the ranges in package.json and installs missing packages. By default, the ranges in package.json don't change. The short form is npm up.

  • npm update express

    Updates only the package you name, within the range defined for it.

  • npm update --save

    Updates the packages and also raises the ranges in package.json to the installed versions.

  • npm install express@latest

    The way to move to a new major version, which npm update won't install because it's outside the range. Before such a move, read the package's changelog and test the application.

  • npm uninstall express

    Removes a package from node_modules and from all the dependency lists in package.json, and updates package-lock.json. The aliases are npm remove, npm rm, and npm un.

  • npm uninstall --no-save express

    Removes the package from disk only, and it stays listed in package.json and in the lock file.

Checking installed packages

  • npm ls

    Shows the project's direct dependencies and the installed versions. The alias is npm list.

  • npm ls --depth=1

    Shows the dependency tree down to the depth you set. --all shows the full tree.

  • npm ls express

    Shows only the paths in the dependency tree that lead to a specific package, including nested copies of it in other versions.

  • npm explain express

    Explains why a package is installed: the chain of dependencies from the project to it. The alias is npm why.

  • npm view express

    Shows information about a package from the registry: description, latest version, dependencies, and tags. The aliases are npm info and npm show.

  • npm view express versions

    Shows all the versions of the package published to the registry.

  • npm view express "dist-tags.latest"

    Shows a specific field from the package information, in this example the version tagged latest.

Scripts and npx

  • "scripts": { "start": "node server.js", "dev": "node --watch server.js" }

    Defines scripts named start and dev in package.json. In scripts, you can call the commands of local packages by name, because npm adds node_modules/.bin to the search path (PATH). The --watch option restarts the application whenever its files change.

  • npm run

    Without a script name, lists the scripts defined in package.json.

  • npm run dev

    Runs a script from package.json. When scripts named predev or postdev are defined, they run before and after the script. The alias is npm run-script.

  • npm run build -- --watch

    Passes options to the script itself. Everything after -- goes to the script and not to npm, and only to the script you named, not to the scripts that run before and after it.

  • npm run build --if-present

    Runs the script only if it's defined, without an error when it's missing. Useful in automated tests shared by several projects.

  • npm start

    Runs the start script. When the script isn't defined and there's a server.js file in the project directory, npm runs node server.js.

  • npm test

    Runs the test script. The short form is npm t.

  • npx eslint .

    Runs a package command: from the project's local packages, or from the registry when the package isn't installed. Before downloading from the registry, npx asks for confirmation, which protects against a typo in the package name. The --yes option confirms in advance, and the --no option declines. npx is based on the npm exec command.

  • node --run dev

    Runs a script from package.json directly through Node.js, without npm. It runs faster, but it's limited: pre and post scripts don't run.

Security: npm audit

  • npm audit

    Checks the project dependencies against the database of security advisories and shows vulnerable packages, the severity of each vulnerability, and how to fix it.

  • npm audit --omit=dev

    Checks only the packages installed on the production server, without the development packages.

  • npm audit --audit-level=high

    Returns an error code only when a vulnerability of high severity or above is found. Suitable for automated tests.

  • npm audit fix

    Installs fixed versions of vulnerable packages, within the ranges defined in package.json. Some vulnerabilities need manual handling.

  • npm audit fix --dry-run

    Shows what npm audit fix would change, without actually changing anything.

  • npm audit fix --force

    Caution: also installs versions outside the ranges in package.json, including new major versions that may break the application. The npm documentation recommends not using it when it isn't clear exactly what will change.

  • npm audit signatures

    Verifies the signatures of the packages downloaded from the registry, and their provenance attestations when they exist.

Cache, settings, and .npmrc

  • npm cache verify

    Checks the integrity of the npm cache and cleans unneeded data from it. By default, the cache is stored in the ~/.npm directory.

  • npm cache clean --force

    Deletes the entire npm cache. Without --force, the command refuses to run, because it's rarely needed: the cache repairs itself. It's recommended to run npm cache verify first.

  • npm config list

    Shows the settings in effect. The -l option also shows the default values.

  • npm config get registry

    Shows the address of the package registry in use. The default is https://registry.npmjs.org/.

  • npm config set registry=https://registry.npmjs.org/

    Sets the address of the package registry. You change it when you work with a private registry, and set it back to this address to return to the public registry. By default, the setting is saved in the user's ~/.npmrc file.

  • npm config delete registry

    Deletes a setting from the configuration files, so the default value applies again.

  • npm config set save-exact=true

    Makes npm save exact versions in package.json, without the ^ sign, on every installation.

  • .npmrc

    The npm configuration file. There are four locations: .npmrc in the project directory, the user's ~/.npmrc, the global file $PREFIX/etc/npmrc, and a built-in npm file. The project file overrides the user file, and the user file overrides the global file. In a global installation, the project file isn't read.

  • //registry.npmjs.org/:_authToken=${NPM_TOKEN}

    An authentication line in .npmrc for a private registry or an npm account. The token is tied to the registry address, so npm won't send it to another registry. ${NPM_TOKEN} is read from an environment variable, so the token itself isn't saved in a file that goes into the code repository.

Version ranges

  • 1.2.3

    An exact version: npm installs only this version.

  • ^1.2.3

    Versions compatible with 1.2.3: minor and patch updates within the same major version, which means from 1.2.3 up to, but not including, 2.0.0. This is the range npm saves by default when you install a package.

  • ~1.2.3

    Patch updates only: from 1.2.3 up to, but not including, 1.3.0. More cautious than ^.

  • 1.2.x

    Any version that starts with 1.2, for example 1.2.0 and 1.2.1, but not 1.3.0.

  • >=1.2.0 <2.0.0

    A version range with a lower and an upper bound. You can use >, >=, <, and <=. Caution: an open range such as >=1.2.0 on its own may later install an incompatible major version.

  • ^1.2.3 || ^2.0.0

    Two ranges, where || means "or": it's enough for the version to match one of them.

  • *

    Any version. Not recommended for regular packages, because an update may install a new, incompatible major version.

npm works with two files. In the package.json file, you define which packages are required and in which version ranges, and the package-lock.json file records the exact dependency tree that was actually installed. Commit package-lock.json to the code repository, so everyone who works on the project, and the server too, gets the same versions. The node_modules directory stays out of the repository, because the installation recreates it. In the development environment, you install and update with npm install and npm update, and on the server you run npm ci --omit=dev.

On a production server, start the application with the NODE_ENV=production environment variable, as the Node.js documentation recommends, because some libraries behave as in a development environment when the variable isn't set. Run npm as the user the application runs as, not as root, and before you update packages in a live application, run npm audit and test the application in the development environment. Support dates for each Node.js version are on the Node.js lifecycle page.

Alongside npm, there are also the pnpm and Yarn package managers, which work in a similar way. In pnpm, you install the project dependencies with the pnpm install command and add a package with the pnpm add command, and in Yarn, the matching commands are yarn install and yarn add. Each of them has its own lock file, so a single project works with one package manager.

Did you know?

On a Linux VPS, you connect over SSH with full root access, install Node.js, and run every command on this page. On a Node.js VPS, the environment is ready in advance: Node.js comes preinstalled and configured, so you can start deploying your application right away.

Node.js VPS plans

npm commands FAQ

What's the difference between npm install and npm ci?

The npm install command installs the dependencies and, if the versions in package-lock.json no longer match package.json, it resolves new versions and updates the lock file. The npm ci command installs exactly what's recorded in package-lock.json, deletes the node_modules directory first, and never changes the files. That's why you work with npm install in the development environment and run npm ci on a production server and in automated tests.

Should I commit package-lock.json to the code repository?

Yes. The package-lock.json file records the exact dependency tree, and when it's in the code repository, every installation, on the development machine and on the server, gets the same versions. This way, what you tested in the development environment is what runs in the application. The node_modules directory, on the other hand, stays out of the repository, because npm ci recreates it.

How do I install a Node.js application on a production server?

Upload the code to the server together with package.json and package-lock.json, and run npm ci --omit=dev. This installs only the packages the application needs, in the versions you tested. Start the application with NODE_ENV=production, as a permanent service that restarts automatically. On a Node.js VPS, the environment is already ready, with Node.js preinstalled and configured.

What do ^ and ~ mean in version ranges?

The ^1.2.3 range allows minor and patch updates within the same major version, which means from 1.2.3 up to, but not including, 2.0.0. The ~1.2.3 range is more cautious and allows only patch updates, up to, but not including, 1.3.0. When you install a package, npm saves it with ^ by default, and the npm update command updates only within the range.

Why does npm skip a package's install scripts?

In recent versions of npm, install scripts of dependencies are blocked by default, so a package can't run code on the server without approval. At the end of the installation, npm shows which packages were skipped. After you check the package, approve it with the npm install-scripts approve command and the package name, and the approval is saved in package.json.

Shopping cart

Your cart is emptyYou can start with one of our popular plans.

Site search

Accessibility

Text size
100%
Accessibility statement