npm is the package manager for javascript. The npm command-line tool is bundled with Node.js. If you have it installed, then you already have npm too. If not, download Node.js.
You can browse packages on http://www.npmjs.com npm-logo

Install a package: npm install

The npm install command installs a package, and any packages that it depends on.

$ npm install -g <PACKAGE_NAME>

The -g flag above tells npm to install the package globally so it can be accessed from anywhere on your machine (defaults to /usr/local/lib/node_modules/phonegap on Mac). Otherwise it will be installed in the node_modules subdirectory of the current working directory.

Recording Updates with –save or –save-dev

  • –save (-S): Package will be saved to your dependencies.
  • –save-dev (-D): Package will be saved to your devDependencies.

Is this package installed and which version ?: npm ls (alias list)

The npm ls command will print to stdout all the versions of packages that are installed.

$ npm ls request
ui-employee@0.0.0 /Users/victor/Dvpt/PROJECTS/ui-Employee
└─┬ gulp-less@3.0.5
  └─┬ less@2.5.3
    └── request@2.67.0

Don’t forget to use “-g” option to check global package.

Use --depth 0 option to not display dependencies.

Lastest available version of package: npm view (alias info)

The npm view command shows data about a package and prints it to the stream. For latest available version use following command:

$ npm view request version
2.65.0

For the full list of available data use –json param:

$ npm view phonegap --json

$ npm view phonegap repository.url
git://github.com/phonegap/phonegap-cli.git

$ npm view phonegap description
PhoneGap command-line interface and node.js library.

Check for outdated packages: npm outdated

The npm outdated command will check the registry to see if any (or, specific) installed packages are currently outdated.

$ npm outdated -g
Package            Current  Wanted  Latest  Location
bower                1.6.5   1.7.2   1.7.2  
cordova              5.2.0   5.4.1   5.4.1  
ios-deploy           1.8.3   1.8.4   1.8.4  
ios-sim              4.1.1   5.0.6   5.0.6  
jscs                 2.6.0   2.8.0   2.8.0  
json2csv            2.11.0   3.0.2   3.0.2  
npm                  3.5.0   3.5.4   3.5.3  
npm-check-updates    2.5.4   2.5.6   2.5.6  
phonegap             5.3.8   5.4.0   5.4.0  
update               0.3.6   0.4.1   0.4.1  
yo                   1.5.0   1.6.0   1.6.0
$

Update a package: npm update

The npm update command will update all the packages listed to the latest version (specified by the tag config), respecting semantic versioning parser.

Uninstall Package: npm uninstall

npm uninstall

Difference between tilde(~) and caret(^) in package.json

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.

npm’s semantic versioning parser clarifies the distinction:

~1.2.3 := >=1.2.3-0 <1.3.0-0 “Reasonably close to 1.2.3”.

^1.2.3 := >=1.2.3-0 <2.0.0-0 “Compatible with 1.2.3”.

― isaacs/node-semver (emphasis added)

The difference between “reasonably close” and “compatible” can be traced back to semantic versioning (SemVer) semantics. From the spec:

Given a version number MAJOR.MINOR.PATCH, increment the:

  • MAJOR version when you make incompatible API changes,
  • MINOR version when you add functionality in a backwards-compatible manner, and
  • PATCH version when you make backwards-compatible bug fixes.

― semver.org

Only major versions are allowed to break compatibility, so a developer should be able to switch between minors and patches with ease.

Upgrade your package.json dependencies to the latest versions

npm-check-updates is a command-line tool that allows you to upgrade your package.json or bower.json dependencies to the latest versions, regardless of existing version constraints.

npm-check-updates maintains your existing semantic versioning policies, i.e., it will upgrade your “express”: “^4.11.2” dependency to “express”: “^5.0.0” when express 5.0.0 is released.

$ sudo npm install -g npm-check-updates
$ ncu

 font-awesome                              4.2  →     4.5 
 bootstrap                                 3.2  →     3.3 
 js-md5                                  1.1.0  →   1.1.1 
 angular-carousel                       ~0.3.x  →  ~1.0.x 
 angular-translate                      ~2.7.2  →  ~2.8.1 
 angular-translate-loader-static-files  ~2.7.2  →  ~2.8.1 
 angular-translate-storage-local        ~2.7.2  →  ~2.8.1 
 angular-translate-handler-log          ~2.7.2  →  ~2.8.1 

Run with -u to upgrade bower.json

Where does npm install packages?: npm root

You can run npm root to see where libraries are installed. Add -g option for global libraries.

Uninstall node and npm from Mac OSX

If installed w/ brew

$ brew uninstall node

If installed with the binary from Nodejs.org

$ sudo rm -rf /usr/local/{bin/{node,npm},lib/node_modules/npm,lib/node,share/man/*/node.*}

Uninstall only npm

sudo npm uninstall npm -g https://docs.npmjs.com/misc/removing-npm

Known Issues


Victor Dias

Sharing mobile Experiences

Follow me