Node JS – NPM – Package Manager

NPM – Node Package Manager : npm makes it easy for JavaScript developers to share and reuse code, and it makes it easy to update the code that you’re sharing.
The official documention is available at : http://docs.npmjs.com
https://www.npmjs.org – centralized repository of public modules

NPM modes of operation

global / local – Node modules can be installed locally or globally. Global installation of modules makes them available throughout any NodeJS Application you are writing on the same system. Local installation includes the module files into the same working directory and makes them available to your application (in the working directory). Default mode of operation is local -> doesn’t make system wide changes – always use the default local mode if you are in a doubt

NPM – The Global Mode

All packages will be installed into /usr/local/lib/node_modules, uses –g flag for Global mode. For example:

npm install -g sax

in any script file, you can then use sax module by using var sax = require(‘sax’);

 

NPM – The Local Mode

default mode, downloads the modules into node_modules directory of your current working directory. For example:

npm install sax

 

NPM – Installing specific version of the module

npm install sax@0.2.5   // will install a 0.2.5 version
npm install sax@0.2.x   // installs the latest release of the 0.2 branch
npm install sax@"<0.3"  // latest version before 0.3
npm install sax@">=0.1.0 <0.3.1" // more complicated requirements

 

NPM – Uninstalling a Module

npm uninstall <package name>         // removes a module locally
npm -g uninstall <package name>      // removes a module globally

 

NPM – Updating a Module

npm update <package name>     // updates an installed module, if the package was not installed the command will install it
npm update -g <package name>  // update a globally installed module

 

Package.json

Basically this can be considered as an alternative to composer.

package.json  can be use this one to define the dependencies, which will be then installed by executing the following command in your working directory:

npm install

example of using package.json:

{
   "name" : "MyApp",
   "version" : "1.0.0",
   "dependencies" : {
      "sax" : "0.3.x",
      "nano" : "*",
      "request" : ">0.2.0"
   }
}

Majd Arbash

...

Leave a Reply