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

Sources:
https://www.themarketingtechnologist.co/how-to-update-an-out-of-date-package-json/
http://www.kubilayerdogan.net/node-js-1-initialize-a-node-js-project-with-dependencies/

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.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA