Wednesday, February 1, 2012

NodeJs : Create and Install Simple Module using npm - Node Package Manager

Let’s see, how to create simple module and how to install the module using npm (Node Package Manager).

In NodeJs, module is a piece of code/function which is doing specific task or operation and modules are higher level of reusable code. Modules are expanding features of project and they are created in either in JavaScript or c++. 

Here I am going to show creation and installation of "Hello world" module. Let’s assume that we already created a project namely "helloworld_proj" and the project has a file namely "index.js" with following code.

Now I am going to create a module namely "helloworld_mod" to display "Hello World" in browser.

1. Create a folder named "helloworld_mod" in your project directory. Obviously you can keep your module anywhere in your system when it is in development stage.

2. And create a file "package.json"  and a folder named "lib" within the module folder.

3. Write following line of code in "package.json"  file.

Note: To know more about "package.json" click here

4. And create a file named "index.js" within "lib" folder. Now the path of "index.js" file would be "C:\Program Files\nodejs\node_builds\helloworld_proj\helloworld_mod\lib\index.js".

5. And then write following code in index.js (located within "lib" folder).

6. Now your module ready to install. To install go to DOS-Command prompt and set base path to your project by using command >cd "C:\Program Files\nodejs\node_builds\helloworld_proj".

7. Now type the command >npm root. This gives path which is currently selected nodejs servers installation path. If it shows the path "C:\Program Files\nodejs\node_modules", that means NodeJs server install the module common for all project. Here assume that you want the "helloworld_mod" module only for "helloworld_proj".

Note : To know more about npm click here

8. To install module within my "helloworld_proj", create a folder named "node_modules" within the project folder. Now the path would be "C:\Program Files\nodejs\node_builds\helloworld_proj\node_modules".

9. Now again type the command >npm root and verify the installation path. It should be "C:\Program Files\nodejs\node_builds\helloworld_proj\node_modules".

10. Now type the command >npm install helloworld_mod. This command install helloworld_mod and displays the detail "helloworld_mod@0.0.1 ./node_modules/helloworld_mod".

11. To confirm the installation, go to "C:\Program Files\nodejs\node_builds\helloworld_proj\node_modules" and you can see new folder with name "helloworld_mod" which is contain files and folders similar to your original module folder.

12. Now go to project folder "C:\Program Files\nodejs\node_builds\helloworld_proj"  and edit the file "index.js" as follows.

13. Start the server by type the command >node index.js . Now type the url in browser and press enter. Then the browser says "helloworld_mod says Hello World".  

For more details click here

Note: Please write your queries and suggestion as comment.

No comments:

Post a Comment