Migrating medium-sized project to Node 4

With all the confusion with io.js has been settled, and epic merge has been done, it's time to upgrade your current project. To bring a medium-sized project to the latest version of Node (as I am writing it's 4.1.2) can be a tedious process, but it allows us to enjoy newer V8 engine and ECMAScript features.

In the end, I reviewed was it worth it and what are the benefits.

Let's start with the Node upgrade.

Node upgrade

I had a Node version 0.12.6 and upgraded it to the latest version. After running the installer, I verified the upgrade by running a command: node --version. That's it!

npm install using Node 4.1

Fingers crossed; I run the npm install on my current project.

The first initial reaction was that we're screwed, those V8 errors are out my expertise. After recovering from the shock, I started to investigate what where the modules that were incompatible and caused the errors shown on the screen.

Contextify

Turn an object into a V8 execution context. A contextified object acts as the global 'this' when executing scripts in its context.

The description didn't help much. Next step was to check which module(s) required contextify.

As I am using npm 3 and its flat package structure I could not just check which folder has a sub-folder contextify. I had to search inside each package.json that are inside nodemodules folder. The easiest way for me was to open Sublime Editor to nodemodules folder, right-click on a folder opens a context menu with the selection "Find in Folder..". Search for string ending with 'contextify"'..

C3.js -> D3.js -> jsdom -> contextify

After digging through dependency chains, release notes, pull requests, etc. I figured out that the C3.js (D3-based reusable chart library) is using older version of D3.js which still relies on older version of jsdom. On later versions of jsdom, contextify isn't required anymore. GitHub user kahboom has made Pull Request to the C3.js project that changes package.json to use newer version of D3.js.

Changes C3 caused to the package.json file

I switched our package.json to use that commit until the Pull Request has been merged:

Gulp-sass -> Node-sass -> libsass

I had to update gulp-sass 2.0.4 to 2.1.0-beta to get latest version libsass and I think this PR was the key thing to get sass compilation working.

Conclusion

There is still a lot of tests to be done, but everything looks promising at the moment. I am a bit hesitant on using ES2015 features just yet as there might be still some issues that might force us to use 0.12.x branch. Babel is always one solution if we need to go back for some reason.

It would be interesting to measure if there are some performance improvements in a real-life project. I found an article that gave positive news:

The request performance is roughly 25% better than 0.12"

I would like to hear your experiences on migrating to Node 4. Just a npm install and everything fine or was it a pain in the butt?

After a storm comes a calm. -Matthew Henry

Hi there! I'm Tatu, an independent Web developer; I help clients on building successful SaaS products. If you're interested in freelancing, you should check my email list How to get started as a freelancer. In my email list, I share private learnings from my own journey, including information about pricing, finding clients, working with the clients, how to get started, etc.
Please don't hesitate to reach out by sending an email or via Twitter.

Are you a Web Developer and been considering freelancing?

In my email list I share private learnings from my own journey, including information about pricing, finding clients, working with the clients, how to get started, etc. Sign up now to learn more + to personally ask me questions!