home
npm-yarn cheat sheet
Dependency management for NodeJS
print

NPM package managment

Install package via NPM

npm install <modulename> --save
npm install <modulename>@<version> --save
# install globally :
npm install --global <modulename>

Install dev package via NPM

npm install <modulename> --save-dev

Add default '--save' to config

npm config set save true

Install package from git repo

npm install git://github.com/my/dep.git

Uninstall package

npm uninstall <module_name>

NPM project initialization

npm init

NPM configuration

get configuration

npm config ls -l
npm config list
npm config get <conf-key>

edit configuration

npm config set <conf-key>=value
npm config edit
npm config set proxy=proxy.dns.fr:3128
npm config set https-proxy=proxy.dns.fr:3128

Update global version

npm update npm -g

NPM scripts

<package.json>
"scripts" : {"the-script" : "node testfile.js"}

Run known or custom script :

npm test
npm run my-custom-script
  • prepublish, publish, postpublish

Run while the package is published

  • preinstall, install, postinstall

Run while the package is installed

  • preuninstall, uninstall, postuninstall

Run while the package is uninstalled

  • prestop, stop, poststop

Run by the npm stop command

  • pretest, test, posttest

Run by the npm test command

  • prestart, start, poststart

Run by the npm start command

  • prerestart, restart, postrestart

Run by the npm restart command (if no 'restart' script, run 'stop' and 'start')

Publish

npm adduser [--registy=<registry>]
npm login [--registy=<registry>]
npm publish [--registy=<registry>]

List outdated package

npm outdated

Linking

npm link

Link allow to create a globally-installed package that is in development (or not in registries).

But you can also link your package in other node_modules directory :

cd my-lib/
npm link
cd ../my-project-that-use-lib/
npm link my-lib

Or in only one step :

cd my-project-that-use-lib/
npm link ../my-lib

Cache clean

npm cache clean

Install in production

This command don't install dev dependencies

npm install --production

Yarn package managment

Yarn is a faster package manager than NPM.

Equivalent to npm command

yarn = npm install
yarn add <package> = npm install <package> --save
yarn remove <package> = npm uninstall <package> --save
yarn add <package> --dev = npm install <package> --save-dev
yarn update = npm update --save
yarn global add <package> = npm install <package> --global
yarn cache clean = npm cache clean
yarn --production = npm install --production
yarn link = npm link

Same command as NPM's

yarn init
yarn outdated
yarn publish
yarn run
yarn login
yarn test

Things yarn has that NPM doesn’t

Allow selective upgrade

yarn upgrade-interactive

yarnInteractive