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

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


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