NodeJS – npm beginner tutorial

Npm (Node package manager) is used to install/uninstall and manage node packages. Here are some common commands when using npm on command line on Linux (Ubuntu) or Mac. Note that for Mac sudo can be skipped if node package has been installed as user.

Get info of a package

This will also tell us what is latest version available for a package:

$ npm info phantomjs
npm http GET https://registry.npmjs.org/phantomjs
npm http 200 https://registry.npmjs.org/phantomjs

{ name: 'phantomjs',
  description: 'Headless WebKit with JS API',
  'dist-tags': { latest: '1.9.19', alpha: '2.0.0-alpha' },
...

Install a package locally

This is default option (no -g flag)

$ npm install jslint

Install a package globally

$ sudo npm install -g jslint

Install a specific version package globally

$ sudo npm install -g jslint@0.10.3

Upgrade already installed package globally

$ sudo npm upgrade -g jslint
// or
$ sudo npm update -g jslint

List currently installed packages globally

Use npm ls -g (npm ls aliases: list, la, ll). This will list packages with dependency tree.

$ npm ls -g
/usr/local/lib
└─┬ jslint@0.9.5
  ├── exit@0.1.2
  ├─┬ glob@4.5.3
  │ ├─┬ inflight@1.0.4
  │ │ └── wrappy@1.0.1
  │ ├── inherits@2.0.1
  │ ├─┬ minimatch@2.0.10
  │ │ └─┬ brace-expansion@1.1.2
  │ │   ├── balanced-match@0.3.0
  │ │   └── concat-map@0.0.1
  │ └─┬ once@1.3.3
  │   └── wrappy@1.0.1
  ├─┬ nopt@3.0.6
  │ └── abbrev@1.0.7
  └─┬ readable-stream@1.0.33
    ├── core-util-is@1.0.2
    ├── inherits@2.0.1
    ├── isarray@0.0.1
    └── string_decoder@0.10.31

List only top level packages in dependency tree

$ npm ls -g --depth 0

List a specific package

$ npm ls -g --depth 0 | grep casperjs
├── casperjs@1.1.0-beta5

Uninstall a package globally

$ sudo npm uninstall -g jslint
Share this article: share on Google+ share on facebook share on linkedin tweet this submit to reddit

Comments

Click here to write/view comments