Package Manger in Node.js


The Package manager is like a library for Node.js. From this package manager the classes and function can be importable . The node has repository  www.npmjs.org to upload and download modules for Node.js. The most interesting modules are express and mongodb and more.

We have to install npm manager in our machine .usually this will automatically installed when we install the node.js. If not we can download install the npm manager using linux terminal command
sudo apt-get install npm

Also each module has it’s own website. Which describe the module functions and the classes. For example we have a module call underscore and also this has own page call www.underscore.org this supports lots fo libraries in JavaScript.

After the installation of the npm Package manager we can download the module using the linux terminal command
1) Go to project directory using linux terminal
2) In terminal command : npm install underscore ( underscore is the name of the package)
3) It will go to npm registry, fetch and download into your directory which is a node_module directory. which contain       underscore (Library ) source code
4) If you import the module into your project directory then it can be automatically importable in projects using,
var name = require(“underscore”) // underscore is just a library name

The module read the details using package.json which is in the module directory.

ERROR:
node.js:201
       throw e; // process.nextTick error, or 'error' event on first tick
             ^
Error: Cannot find module 'graceful-fs'
   at Function._resolveFilename (module.js:334:11)
   at Function._load (module.js:279:25)
   at Module.require (module.js:357:17)
   at require (module.js:368:17)
   at Object.<anonymous> (/usr/share/npm/lib/utils/ini.js:32:10)
   at Module._compile (module.js:432:26)
   at Object..js (module.js:450:10)
   at Module.load (module.js:351:31)
   at Function._load (module.js:310:12)
   at Module.require (module.js:357:17)
dhanushanth@ubuntu:~$

If you got any error like above . Then the problem is with the older version of node. So you need to update the node.js version or we have another solution as well.

Solution : This will install the npm according to our node.js version

git clone git://github.com/isaacs/npm.git
cd npm/scripts
chmod +x install.sh
sudo ./install.sh


Npm library global installation
Another place that we can install the node_module globally which means that we can install this once as global
To install that command in terminal,
sudo npm install underscore -g

After the installation we can update the module using below command,
npm update _PackageName_


Search for package
In www.npmjs.org we can search for packages in search package after the search we just need to get the name of the package and install the module using the same way that we have done for unsercore


Note: Always check the updates of the package doc for changes. Because according the update the classes and function calling may be chage.

No comments:

Post a Comment