Skip to content

Node.js / npm mini cheatsheet

Just some really basic commands, but paradoxically they’re not always obvious to find so hopefully they’ll help some newcomers

Running a script with Node.js:
node myscript.js

Initializing a Node app/package:
Move to the target folder (where you want to place your code) and npm init

Installing dependencies (listed in package.json) for the current package:
npm install

Updating dependencies:
1) Quick and dirty: edit package.json and replace version numbers for the dependencies you want to update with "*", then run
npm install --save, or npm install --save-dev for the devDependencies
2) Normal way: for each package, one by one, run:
npm install package-name --save (or --save-dev)
It happened to me in the past that dependencies wouldn’t update when I ran that, in which case you can try to change their version number as described in 1), only one by one, and you could also try deleting your package-lock.json file. You can also force a version number, for instance npm install –save package-name@^2.
3) npm update is also supposed to update dependencies, in the same way as 1) I remember it failing more often than succeeding, but last time I tried it worked nicely
4) My new favorite way: cf later in this post the section about npm-check-updates

Removing some dependencies:
Remove their entries from package.json, then run npm prune

Updating global librairies/packages:
npm update -g

Removing a global package:
npm uninstall -g [package name]

Finding the version of a currently installed global package:
npm list -g [package name]
Note that it may allow you to notice that a package is installed with multiple, different versions, manually and as a dependency of other packages

Checking for updates for all your packages:
There’s a package for that called npm-check-updates (beware, it has tons of dependencies, over 250).
npm i -g npm-check-updates
Then, ncu for the dependencies of a project of ncu -g for global dependencies. There is also a command to update your package.json (then you can run npm i to udpate): ncu -u.

Listing globally installed packages, but only those that you actually installed (not their dependencies, which are probably huge):
npm list -g --depth 0


Update 2021-09-21: finding the npmrc files

A tiny number of damn important Node settings are located in those hard to find, multiple files. Here’s the documentation on where to find them, and here is where I found them in my case:

  • C:\Program Files\nodejs\node_modules\npm\.npmrc => empty
  • C:\Program Files\nodejs\node_modules\npm\npmrc => very same folder but just a removed dot in the name… how fucked up. Anyway, this one was very important as I had to put prefix=${APPDATA}\npm inside of it after some Node upgrade (I believe it was from Node 12 to 14), otherwise global package installation was completely messed up
  • %appdata%\npm\etc\npmrc => not empty but unmodified for almost 4 years… My guess is that’s an ancient location that’s not used anymore?

Posted in JavaScript / TypeScript / Node.js.

0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA