Npm Cheat Sheet

by Kristian Ranstrom
October 4, 2021
2 min read

npm is a cli-based package manager for Node JS.  It comes installed when you install Node.js.  Here are a bunch of how-to scripts to handle common situations:

Install 

Install all items within the package.json or install an individual package into the npm repo.


-- installs anything in the package.json
npm install
npm i 
-- installs anything in the package.json except devDependencies
npm i --production
-- installs an individual package from the npm repo
npm i antd
npm i [email protected]
-- installs a package as a devDependency
npm i --save-dev antd

Usage

Once it's installed, you can run the program by using:

npm start

You can make a build of your project by using:

npm run-script build

Outdated

Find packages that are outdated.  This will display a list of available updates. 

npm outdated

List Packages

List all of the installed packages…this is the same as just looking in the package.json file

npm list

Update Packages

Note that if there is a major update, it will not update to the latest.  To do that, you'll have to use npm i @latest.

-- updates anything in the package.json
npm update
-- updates global packages
npm update -g
– updates an individual package
npm update antd

You will periodically have to update npm itself globally.  This can be done with the following command:

npm i -g [email protected]

Weird Stuff

When I globally updated npm on my Windows machine, I noticed that it didn't really update it.  After digging I noticed that npm was installed in a couple areas in Windows:

  1. C:\Program Files\nodejs\node_modules\npm
  2. C:\Users\redap\AppData\Roaming\npm\node_modules\npm

Updating the npm package globally updated the second location, but the cmd line uses the first location.  That's frustrating.  To fix it, I just copied the contents of the second location to the first location and viola!, it was updated.