NPM - Node Package Manager

  • use nvm, the node version manager, to use several node versions on your machine.
  • if you want to automatically use a specific node version in a project’s directory, avn might be worth a try.

  • use the latest node and npm versions (e.g. node 5.4.1 and npm 3.3.12)

Important npm commands:

  • npm init: initialize a new npm project. Basically runs an interview tool that writes an initial package.json for you.
  • npm install: install all npm packages from package.json
  • npm install my_package_name: install an individual package. Important options:
    • -g to install a package globally (i.e. not in project’s node_modules - only used for very few packages)
    • --save / -S install and add an entry to package.json’s dependencies section (for runtime dependencies)
    • --save-dev / -D install and add an entry to package.json’s devDependencies section (for compile time dependencies)
  • npm prune: clean up node_modules - remove all packages that are no longer listed in package.json.
  • npm shrinkwrap: create an npm-shrinkwrap.json file that lists all actually installed package versions (much like a Gemfile.lock). More info: What is npm shrinkwrap and when is it needed. N.b.: it will not list devDependencies - you can include them with a command line switch, but presumably this leads to other problems and is not recommended.

Execute an npm package’s binary

./node_modules/.bin/babel-node


Package.json

  • npm’s Gemfile
  • Difference between ~ (tilde) and ^ (caret): “In the simplest terms, the tilde matches the most recent minor version (the middle number). ~1.2.3 will match all 1.2.x versions but will miss 1.3.0. The caret, on the other hand, is more relaxed. It will update you to the most recent major version (the first number). ^1.2.3 will match any 1.x.x release including 1.3.0, but will hold off on 2.0.0.” (http://stackoverflow.com/a/22345808)