Npm Cheat Sheet
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 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 antd@latest -- installs a package as a devDependency npm i --save-dev antd
Once it's installed, you can run the program by using:
You can make a build of your project by using:
npm run-script build
Find packages that are outdated. This will display a list of available updates.
List all of the installed packages…this is the same as just looking in the package.json file
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 npm@latest
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:
- C:\Program Files\nodejs\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.