Skip to content

Instantly share code, notes, and snippets.

@AvnerCohen
Last active July 9, 2023 09:14
Show Gist options
  • Save AvnerCohen/4051934 to your computer and use it in GitHub Desktop.
Save AvnerCohen/4051934 to your computer and use it in GitHub Desktop.
Node.js - npm Cheat Sheet

Node.js - npm Cheat Sheet

(Full description and list of commands at - https://npmjs.org/doc/index.html)

List of less common (however useful) NPM commands

Prepand ./bin to your $PATH

Make sure to export your local $PATH and prepand relative ./node_modules/.bin/:

export PATH="$PATH:./node_modules/.bin"

This will allow executing npm binaries installed into the .bin local and isolated current ./node_modules

Install a package and also update package.json with the installed version and package name.
npm install <module-name> --save
Install a package and also update package.json with the installed version and package name, but into the devDependencies section.
npm install <module-name> --save-dev
Set --save as a default for npm install .
npm config set save true
Generate package.json in a module directory, based on npm parmaters.
npm init
List all npm configuration flags.
npm config ls -l
Install a version not from a git repository and not from the npm directory, for example:
npm install git://github.com/substack/node-browserify.git
Update the global npm version.
npm update npm -g
Display the readme.md / documentation / npmjs.orf page of a give library.
npm docs <module-name>
Run package test suite, based on setup in package.json in:

"scripts" : {"test" : "node testfile.js"}

npm test
Uninstall package (A nice thing about npm is you can always just rm -rf ./node_modules/<module_name>).
npm uninstall <module_name>
Locally edit a dependency.
npm edit <module_name>
Setup editor for npm edit :
npm config set editor "sublime"
Publish a package not under the default "latest" tag:
npm publish --tag beta
Test & Show the full dependency tree
npm install --dry-run
List outdated libraries compared to currently installe node_modules:
npm outdated
Lock down dependency versions:
npm shrinkwrap
Install a git specific release
npm install git://github.com/Marak/colors.js#v0.6.0
Easter Eggs
npm xmas
npm visnup
npm substack
@isaacs
Copy link

isaacs commented Nov 10, 2012

This is incorrect:

npm install git@github.com:substack/node-browserify.git

But any of these will work:

npm install git+ssh://git@github.com:substack/node-browserify.git
npm install git://github.com/substack/node-browserify.git
npm install git+http://github.com/substack/node-browserify.git
npm install substack/node-browserify # only in the latest npm release

@AvnerCohen
Copy link
Author

Thanks, corrected.

@catdadcode
Copy link

How do you install a package from a tag? Is it just npm install packageName --tag beta?

@AvnerCohen
Copy link
Author

@chevex Not sure if you mean npm tag or github tag.
I'm not 100% if an npm tag makes any sense, to install a github tag you should use something like this:

npm install git://github.com/[username]/[reponame]#[tag_name]
#example:
npm install git://github.com/Marak/colors.js#v0.6.0

@mikermcneil
Copy link

@chevex meant an "npm tag" I think, re: https://coderwall.com/p/l_7acq. Also like to know if you know :)

@mikermcneil
Copy link

Ah got it ( I think )

To install the package sails, using the snapshot tagged as beta:
npm install sails@beta

source: http://tobyho.com/2012/02/09/tour-of-npm/

EDIT: turns out this works, at least as with:

Merlin:bar mike$ node -v
v0.10.20
Merlin:bar mike$ npm -v
1.3.11

@gregersrygg
Copy link

Here are some of my favourites:

Update all to the latest version of all dependencies according to the version range in package.json (npm install does not check for newer versions when the version in node_modules is within the version range):

npm update

Link to the source-code for a dependency to test changes locally before publishing new versions:

# symlink ~/devel/module-a/node_modules/module-b -> /usr/local/lib/node_modules/module-b -> ~/devel/module-b
~/devel/module-a $ npm link ../module-b

To set default values for npm init:

npm config set init-author-name "Your name"
npm config set init-author-email "your@email.com"
npm config set init-license MIT

To set project-specific config. I.e. publish to a private repo. Create a .npmrc file in the project folder:

registry=http://npm.ourcompany.com/

By default npm uses .gitignore to ignore files to include in the published package. If you need a different set of ignores, use .npmignore as well.

Not part of the npm cli, but I find npm-check a lot easier than npm outdated. Instead of only listing the outdated depenencies, it can print out commands to copy-paste to update each outdated dependency, or use the interactive mode where you just mark the dependencies to update.
interactive npm-check

@DonRichards
Copy link

You could run > npm outdated -g

@vicentedealencar
Copy link

> npm substack
Replace your configs with services

on windows*

@zishon89us
Copy link

Copy link

ghost commented Jul 29, 2017

######Install a version not from a git repository and not from the npm directory, for example:

there are 2 "not"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment