Here are the links to the previous installments:
- Threads vs. Events
- Using Non-Standard Modules
- Debugging with node-inspector
- CommonJS and Creating Custom Modules
- Node Version Management with n
- Taking Baby Steps with Node.js – Implementing Events
- 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.