Posted on by & filed under Content - Highlights and Reviews, Web Development.

In My First Time With Node.js article I wrote an application using Node.js. Follow along if you’ve ever had troubles loading modules in Node.js.

If you’ve played with modules a little, you may have tried your hand at writing your own or installing one that didn’t use npm. At some point in the process, you might have come across some issues with Node not finding your module. Let’s look at what that’s like.

Suppose you’ve written a class to use in the browser, but you want to test it using Node.js as part of a continuous integration build process. If your class uses a browser construct – localStorage, for example – then you might see something like this when you try to run the test:

Luckily, someone’s already written a localStorage implementation for Node.js, so all we have to do is pull that in and we should be good. You can install it using npm, but for the sake of argument, let’s say you didn’t.

If it’s your first time with Node.js, you might think you can just save the localStorage.js file to your working directory and include it using a normal required syntax:

That’s not going to do it. Your syntax is fine, but you’ll need to put the file where Node can find it, or tell Node where it is. That means putting it in the node_modules directory. In my case, when I installed the ws module using npm, a node_modules directory was created in my working directory. Meaning all I had to do was save localStorage.js into the ./node_modules directory.

If you don’t have a node_modules directory in your working directory after using npm to install a module, check or set the NODE_PATH environment variable to indicate where modules belong. Technically, you can require the file using the full path to the JavaScript file. But if you’re going to be working with modules, you should become accustomed to how and where Node.js will be looking for them.

You should now have a better understanding about modules in Node.js.

Safari Books Online has the content you need

Take advantage of these Node.js resources in Safari Books Online:

Node: Up and Running (Rough Cuts) introduces you to Node, the new web development framework written in JavaScript. You’ll learn hands-on how Node makes life easier for experienced JavaScript developers: not only can you work on the front end and back end in the same language, you’ll also have more flexibility in choosing how to divide application logic between client and server.
Read What Is Node? to get up to speed on Node.js with this concise overview.
Node for Front-End Developers shows you how to use this popular JavaScript framework to create simple server applications, communicate with the client, build dynamic pages, work with data, and tackle other tasks.
In Sams Teach Yourself node.js in 24 Hours, expert web developer George Ornbo guides readers through every step of creating custom server-side solutions with Node.js.

About the Author

  Duane O’Brien is a tired computer scientist. He has written a number of articles on developing web applications and various PHP frameworks. To learn more about Duane, check out his blog or read his tweets.

Tags: localStorage, modules, Node.js,

Comments are closed.