Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint

Using Modules

To use modules in your Node.js applications, you must require them after they have been downloaded. Requiring a module in your application looks like this:

var module = require('module');

Now, when you run your application, it will look for the source files for the library and include it in your application. Normally, you want to use the module so it is assigned to a variable.


Try It Yourself

If you downloaded the code examples for this book, this code is available as hour02/example01.

To use third-party modules in Node.js, follow these steps:

1. Open your text editor and write the following:

var _ = require('underscore');
_.each([1, 2, 3], function(num){
  console.log("underscore.js says " + num);
});

2. Save your file as follows:


  

You are currently reading a PREVIEW of this book.

                                                                                        

Get instant access to over
$1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint