{ Introduction to Node.js. }

Objectives:

By the end of this chapter, you should be able to:

  • Compare and contrast client and server side JavaScript
  • Create custom modules with node
  • Define special keywords including require, module and exports

Node JS Introduction

Up until now, we have been writing JavaScript exclusively in the browser, but the browser isn't the only JavaScript environment. Today, we can use JavaScript to build desktop applications, mobile applications, and server-side applications. The technology that enables us to build these types of applications in JavaScript is Node.js.

You can install node.js here. There also exists a tool called nvm, which you can use to manage different versions of node, but for now let's install the LTS version of Node on the website.

You can read more about installing node on Windows here

writing our first node code

In the terminal, create a file called first.js and add the following text:

console.log('Hello World!');

Since we are building applications for the server side, we will be running our code in the Terminal (not the browser). To run this application let's type in the same directory we create our first.js file: node first.js. We should see "Hello World!" appear in our console! (If you are not comfortable using the terminal, make sure you go through our free Terminal and Unix course first.)

require and module.exports

Node.js comes with built-in support for modules. These are not the same as ES2015 modules, these are called common.js modules and have a syntax that makes use of the require, module and exports keywords. Modules allow us to write code in one file and export that code to another. Since we are not using script tags (as we are not in the browser), we need a way of loading JavaScript from one file to another, and modules are our answer.

Let's take a look at some examples using Node.js modules. Create a file called helpers.js, which will export some data to another file called main.js. In our helpers.js, let's add the following:

module.exports = {
    name: "Elie",
    sayHi(){
        console.log(`Hi ${this.name}!`);
    }
}

Now in our main.js, let's add the following:

var helpers = require('./helpers');
helpers.sayHi();

If we run node main.js we should see the text "Hi Elie!" in the console. So how have we done this? We used the module.exports object to send an object to another file (main.js). To import the module we use the require keyword followed by the name of a module. Notice that we are using a relative path here. This is very important, because if we do not use a relative path, Node will try to find a module that either comes with Node.js or that we have installed externally. When you are using your own modules, always use relative paths.

Now let's look at some other syntax for exporting modules. Let's make a file called moreHelpers.js and let's use the exports syntax.

exports.sayHello = function(){
    console.log('Hello!');
}

exports.sayGoodbye = function(){
    console.log('Goodbye!');
}

exports.firstName = "Elie";

We can see above that instead of wrapping everything inside of the module object, we can attach values to the exports object and make use of it in a very similar way. Let's require this module back in our main.js

var helpers = require('./helpers');
var moreHelpers = require('./moreHelpers');
helpers.sayHi();

console.log(moreHelpers.firstName);
moreHelpers.sayHello();
moreHelpers.sayGoodbye();

Great! Now let's see one more use case where we simply export a function and then make use of it. Let's create a new file called evenMoreHelpers.js

module.exports = function(){
    console.log('I am the entire module!');
}

Here we are simply exporting a function as the module, so when we require this module, it will not be an object, it will just be that function!

var helpers = require('./helpers');
var moreHelpers = require('./moreHelpers');
var evenMoreHelpers = require('./evenMoreHelpers');

helpers.sayHi();

console.log(moreHelpers.firstName);
moreHelpers.sayHello();
moreHelpers.sayGoodbye();

evenMoreHelpers(); // this is a function on its own!

When you're ready, move on to Core Node.js Modules

Continue