Node.js: Using require to load your own files

December 31, 2012

For a lot of JavaScript developers that are moving over from traditional "browser" development to node.js, they might be casually aware of the require keyword that node.js provides you.

If you're wondering what I'm talking about, allow me to explain.  In node.js, you can use the node package manager (NPM) to install packages that your application can use.  Express, for example, is a great framework for serving up web applications, and you can grab it via NPM.

To load Express into your application, you tell node.js that you "require" it be loading into memory.

var express = require("express");

Node follows a couple rules to find modules.  First, you can ask for a module by name or by folder.  Let's look at Express more closely.  If you were to download Express via NPM, you'd find there is no express.js file in the root directly of the /node_modules/express folder.  However, there is a package.json file that defines the main executable file for express (for fun, go look it up).

Now, if packages.json doesn't exist or the main executable isn't present, node will resort to looking for your filename with either a .js, .json, or .node extension (unless you've specified).

Where's this going?

I know, I know... the point.

Let's say you want to abstract out a piece of your application into another file.

var userRepository = function (){
   var self = this;
   self.addUser = function (...){
   };
   self.get = function (...){
   }
};

module.exports = userRepository; Add this to a file called userRepository.js. The last line is VERY IMPORTANT! It tells node.js what you'd like to export from this file. This should make more since if you try to use the file.

In your main.js or wherever you'd like to use userRepository:

var userRepository = require("userRepository.js");

var userRepositoryInstance = new userRepository();
userRepositoryInstance.addUser({...});
userRepositoryInstance.get(...);

Looks simple doesn't it? Pretty much whatever you assign to module.exports will be passed into the variable that calls require().

Use this to keep your code clean and uncluttered.  This is only the basics of using require for your own projects, but I think it's a great starting point for developers building their knowledge of node.js.  In the future, I'd like to expand on this topic and show you how you can take this even farther.