How to deal with cyclic dependencies in Node.js

I’ve been working with nodejs lately and still getting to grips with the module system so apologies if this is an obvious question. I want code roughly like the following below: a.js (the main file run with node) var ClassB = require(“./b”); var ClassA = function() { this.thing = new ClassB(); this.property = 5; } … Read more

Nodejs cannot find installed module on Windows

I am learning nodejs at the moment on Windows. Several modules are installed globally with npm.cmd, and nodejs failed to find the installed modules. Take jade for example, npm install jade -g Jade is installed in directory “C:\Program Files (x86)\nodejs\node_modules”, but the following code will fail with a “Cannot find module ‘jade'” error, var jade … Read more

How to make node.js require absolute? (instead of relative)

I would like to require my files always by the root of my project and not relative to the current module. For example if you look at https://github.com/visionmedia/express/blob/2820f2227de0229c5d7f28009aa432f9f3a7b5f9/examples/downloads/app.js line 6 you will see express = require(‘../../’) That’s really bad IMO. Imagine I would like to put all my examples closer to the root only by … Read more

The difference between “require(x)” and “import x”

I’ve just started working on a small node project that will interface with a MongoDB. However, I cannot seem to get the relevant node modules to import correctly, even though I have installed them correctly via npm. For example, the following code throws an error, telling me that “express has no default export”: import express … Read more