Blog

  • Home /
  • Blog /
  • Taking Baby Steps with Node.js – “node_modules” Folders

Taking Baby Steps with Node.js – “node_modules” Folders

March 17, 2011

Here are the links to the previous installments:

  1. Introduction
  2. Threads vs. Events
  3. Using Non-Standard Modules
  4. Debugging with node-inspector
  5. CommonJS and Creating Custom Modules
  6. Node Version Management with n
  7. Taking Baby Steps with Node.js – Implementing Events
  8. BDD Style Unit Tests with Jasmine-Node Sprinkled With Some Should

For this post I want to quickly share a nice addition to Node.js that is available since version 4.0.x. In the previous post, I provided some example code of BDD style unit tests that make use of the should.js library that enabled us to use BDD style assertions. We loaded the ‘should’ module just as if it was a native module:

In order to accomplish the same using version 0.2.x of Node.js, we needed to either prefix a relative path:

or add our dependencies folder to the require.paths:

var should = require('should');

When you omit the “/”, “../” or “./” prefix for loading a non-native module using version 4.0.x, then Node.js will automatically start searching in the parent directory of the current module for a folder named “node_modules” and tries to load the requested module from that location on disk. If it cannot be found there, then it goes up one level again and repeats the same process until the module is found or until the root folder is reached.

For that reason I renamed all my “dependencies” folders to “node_modules” so I that I’m able to incorporate third-party modules more easily.

This might not sound like a big deal, and it certainly isn’t, but this small new feature already reduced a good number of WTF’s on my end ;-). 

Until next time.

If you and your team want to learn more about how to write maintainable unit tests and get the most out of TDD practices, make sure to have look at our trainings and workshops or check out the books section. Feel free to reach out at infonull@nullprincipal-itnull.be.

Profile picture of Jan Van Ryswyck

Jan Van Ryswyck

Thank you for visiting my blog. I’m a professional software developer since Y2K. A blogger since Y2K+5. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Past organizer of the European Virtual ALT.NET meetings. Thinking and learning about all kinds of technologies since forever.

Comments

About

Thank you for visiting my website. I’m a professional software developer since Y2K. A blogger since Y2K+5. Author of Writing Maintainable Unit Tests. Provider of training and coaching in XP practices. Curator of the Awesome Talks list. Thinking and learning about all kinds of technologies since forever.

Contact information

(+32) 496 38 00 82

infonull@nullprincipal-itnull.be