A pain in the node!
I was recently playing with a node.js and Express application and ran into a small issue that wouldn’t let me render any templated content to the client. The issue was presenting itself in a number of ways, firstly:
The first error I was getting was:
app.configure(function() {
^
TypeError: Object function (req, res, next) {
app.handle(req, res, next);
} has no method ‘configure’
If you look online for express documentation, most will be using the app.configure syntax. However, as of version 4.0.0 of express, the app.configure method has been removed. See here (these release notes) for more detail.
Looking in my package.json file I had the dependency for express set to latest:
1 2 3 4 |
"dependencies": { "express": "latest", "jade": "~1.3.1" } |
This is inherently dangerous for this very reason. As node and express are evolving so quickly, the likelihood of breaking changes between versions is great. The quick fix for me was to enforce an older (compatible) version that I know worked with the rest of the app:
1 2 3 4 |
"dependencies": { "express": "3.4.0", "jade": "~1.3.1" } |
Then, once I rolled back the version of express (this is only a test app – the forward thinking thing to do would be fix the now obsolete code!) I started getting the following error:
Error: No default engine was specified and no extension was provided
This is strange as I clearly had specified a view engine:
1 2 3 4 |
app.configure(function () { app.set('view engine', 'jade'); app.set('views', __dirname + 'srv/views/'); }); |
After searching online and reading lots of solutions (most related back to daft errors like this transpired), I started to doubt the validity of the error message. Turns out that it was in fact the path to the views folder that was failing and it was nothing to do with the view engine (jade) at all.
A quick fix to the path and everything started working:
1 2 3 4 |
app.configure(function () { app.set('view engine', 'jade'); app.set('views', __dirname + '/srv/views'); }); |
The simplest mistakes often cause the biggest headaches.
Despite its rapid change/release cycle and constantly battling with new versions and breaking changes, I do love the ‘bare metal’ feel of programming in Node.