Difference between revisions of "Creating a Node module"

From Logic Wiki
Jump to: navigation, search
(Created page with "Category:Javascript Category:Node.js We can share any functionality in our code by exporting it using module.exports == Lets make a Hello World module! == Create a...")
(No difference)

Revision as of 10:39, 19 September 2016


We can share any functionality in our code by exporting it using module.exports

Lets make a Hello World module!

Create a directory hello-world and in that create a file index.js and app.js.

mkdir hello-world
cd hello-world
touch index.js
touch app.js

Open index.js and add a function that takes in a Name and returns the value 'Hello Name!'

function (name) {
  console.log("Hello " + name + "!");
}

Now, turn this function into a module that can be used in another file, by using module.exports:

module.exports = function (name) {
  console.log("Hello " + name + "!");
}

Open app.js and use the exported function by using the require() statement:

var hello = require('./index');
hello('World');

The './' before index says that the file is in the same directory, and the '.js' is not required.

Now, go to the command line and run app.js:

node app.js

This should print 'Hello World!' on the command line.

Publishing a Node Module to NPM

In the previous section, we created a node module that can be "required" by another file in our codebase, but what if we cant to make our module available to the world? We can publish it to the NPM package repository such that anyone can use it. Lets do that...

  • Go to npmjs.com and create a new account
  • Go to the command line and enter the command: npm login. This will prompt you for the username and password for the account you just created. You will need to login in order to publish the module.
  • Enter the command: npm init. This will present a series of questions regarding your package. It is important to give your package name a unique name (something that no one else has used before). I picked 'fst-hello-world', you can try something similar. After answering all the questions, this will create a 'package.json' file in that directory. This file holds all the details needed for you to publish and others to install your package.
  • Enter the command: npm publish. Once published, you can go to https://www.npmjs.com/package/your-hello-world to see your published package.

Installing & Using a published Node Module

Lets try using our just published module.

Create a directory hello-world-test and in that create a file app.js.

mkdir hello-world-test
cd hello-world-test
touch app.js

Now, install the module that you just published.

npm install your-hello-world

The above command will create a directory node_modules in your current directory, and install your module in it.

Now, open app.js and this time instead of requiring it as a file, we will require it using the module name:

var hello = require('your-hello-world');
hello('World');

Now, go to the command line and run:

node app.js

It will print 'Hello World!' on the command line.

By following the steps in this section, you can use any of the more than 200K modules in the NPM repository like jQuery, lodash and more.

Installing a Node Module globally

Node Modules can also be built and packaged to work as command line applications. Such modules can be installed globally and then used as commands. For this, we need to add a -g option to the install command like below:

npm install <module-name> -g