npm commands cheat sheet
Versions and help
-
node -vShows the version of Node.js installed on the server. The full form is node --version.
-
npm -vShows the npm version. The full form is npm --version.
-
npm helpShows the list of npm commands.
-
npm install -hShows a usage summary for a specific command, in this example install.
New projects and package.json
-
npm initCreates 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 -yCreates package.json with the default values, without asking questions. -y is short for --yes.
-
npm pkg get scripts.testReads 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": trueA 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 installInstalls 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 expressInstalls 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.21Installs 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 eslintAdds 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 expressSaves an exact version in package.json, without the ^ sign. -E is short for --save-exact.
-
npm install --no-save expressInstalls a package into the node_modules directory without adding it to package.json.
-
npm install --ignore-scriptsInstalls 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 pm2Installs 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 ~/.localMoves 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 -gLists the installed global packages.
-
npm root -gShows the node_modules directory where global packages are stored. Without -g, it shows the node_modules directory of the current project.
-
npm update -gUpdates 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 pm2Removes a global package.
npm ci and production installs
-
npm ciA 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=devInstalls 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=devInstalls 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=devRemoves 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 lsIn 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 sharpAllows 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 sharpRecords 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 outdatedShows 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 updateUpdates 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 expressUpdates only the package you name, within the range defined for it.
-
npm update --saveUpdates the packages and also raises the ranges in package.json to the installed versions.
-
npm install express@latestThe 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 expressRemoves 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 expressRemoves the package from disk only, and it stays listed in package.json and in the lock file.
Checking installed packages
-
npm lsShows the project's direct dependencies and the installed versions. The alias is npm list.
-
npm ls --depth=1Shows the dependency tree down to the depth you set. --all shows the full tree.
-
npm ls expressShows only the paths in the dependency tree that lead to a specific package, including nested copies of it in other versions.
-
npm explain expressExplains why a package is installed: the chain of dependencies from the project to it. The alias is npm why.
-
npm view expressShows information about a package from the registry: description, latest version, dependencies, and tags. The aliases are npm info and npm show.
-
npm view express versionsShows 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 runWithout a script name, lists the scripts defined in package.json.
-
npm run devRuns 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 -- --watchPasses 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-presentRuns the script only if it's defined, without an error when it's missing. Useful in automated tests shared by several projects.
-
npm startRuns 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 testRuns 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 devRuns 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 auditChecks 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=devChecks only the packages installed on the production server, without the development packages.
-
npm audit --audit-level=highReturns an error code only when a vulnerability of high severity or above is found. Suitable for automated tests.
-
npm audit fixInstalls fixed versions of vulnerable packages, within the ranges defined in package.json. Some vulnerabilities need manual handling.
-
npm audit fix --dry-runShows what npm audit fix would change, without actually changing anything.
-
npm audit fix --forceCaution: 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 signaturesVerifies the signatures of the packages downloaded from the registry, and their provenance attestations when they exist.
Cache, settings, and .npmrc
-
npm cache verifyChecks 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 --forceDeletes 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 listShows the settings in effect. The -l option also shows the default values.
-
npm config get registryShows 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 registryDeletes a setting from the configuration files, so the default value applies again.
-
npm config set save-exact=trueMakes npm save exact versions in package.json, without the ^ sign, on every installation.
-
.npmrcThe 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.3An exact version: npm installs only this version.
-
^1.2.3Versions 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.3Patch updates only: from 1.2.3 up to, but not including, 1.3.0. More cautious than ^.
-
1.2.xAny 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.0A 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.0Two 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.
No matching commands.
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.
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.
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.