npm upgrade

npm is a great library. It really is. But it has terrible error messages.

The console always says something about finding more information in npm-debug.log, but this information is rarely helpful.

Even if i use –loglevel silly flag, i get dozens of lines that don’t mean much, and then the same meaningless error message i got without using any flag at all.

Googling doesn’t do much good either, because those messages are so generic.

when i couldn’t publish my package today i  eventually decided to dive into the npm package to see for myself the line where the code broke.

Instead of looking on my machine, i found it easier to browse the npm github repository and look for the misbehaving line.

Guess what? The line wasn’t there.

Well dah. The npm version i was using turned up to belong in a museum.

OK then, i set out to upgrade npm. shouldn’t be easier. It’s a node package, right?

npm install npm@latest -g should do the trick, right?

Well, not on Windows, apparently.

I came up with this link:

i installed Microsoft’s windows upgrade script, that turned out to be as buggy as you would expect a Microsoft product.

Then I tried doing what the script tried to do manually. This didn’t work either.

Eventually I had to reinstall node.js. That upgraded my npm version, and solved the original problem as well.


Read Only Variables in Javascript

It is not recommended to define variables in the global scope.

However, if you need to, you should prevent the scenario that another developer (or you someplace else in your code) will override it.

If you don’t have to support old browsers, use Object.defineProperty:

Object.defineProperty(window,”e5Var”,{value: 1, writeable: false});
e5Var = 2;
console.log(e5Var); //1

Still, if someone redeclares the variable further in the code using “var” it will not work.

Object.defineProperty(window,”e5Var”,{value: 1, writeable: false});
var e5Var = 2;
console.log(e5Var); //2

Déjà vu

Some time ago i experienced a very baffling phenomenon when debugging my node.js application.
after some time, node started rerunning the code that had already been executed.
I had no idea what could cause this behavior!
Turns out that when the response times out, node.js re-sends the request.
In order to prevent this problem, when you have a script with long execution time, use res.setTimeout with a large number.

Function Templates at Work

Ever since I’ve read Angus Croll’s post “Understanding javascript closures” I’ve been intrigued by the idea of creating function templates with closures, but until now it was never the suitable solution for my needs.

Last week the opportunity finally showed up…

I have a web application that does a lot of asynchronous work on the server, and uses to communicate the progress status to the client.

I was asked to create a REST API for this application.

The challenge was to keep the usage the same in the web app, while supressing the progress report in the REST API and sending JSON to the response on success or failure.

The web app needs to be available always in order to emit messages. The API needs the response stream available when  the process ends.

Passing these variables to all of the functions would be possible, but not very elegant.

The solution I chose was to encapsulate the communication function.

for the web app I created a function template that closes on the socket id, and for the API I created a function template that closes on the “res” variable.

The products of these templates were passed as parameters to the functions that do the actual work, and called whenever the progress status changed.

This is a simplified version of the result :

exports.startProcessAPI = function (req, res) {

var templateReportFunc = function (res)  {

return function (report) {



},reportFunc = templateReportFunc(res);



exports.startProcess = function (socket) {

var templateReportFunc = function (socket_id)  {

return function (report) {

io.sockets[socket_id].emit("report sent", report);


},reportFunc = templateReportFunc(;




Creating function templates in order to “close” on variables that are useful further in the code’s execution can be very helpful, especially when there are separate execution paths that require different variables.