Josh Bavari's Thoughts

Thoughts on technology and philosophy

Understanding Built Node Modules

about a 1 minute read

If you’ve recently change node versions and begun running into issues with some of your modules, you might get a little help from understanding how native node modules work.

TL;DR: If you upgraded node, run npm rebuild or rm -rf node_modules && npm install.

Why: This has to do with some of your modules you may be using having native bindings to your current system runtime.

This then put me on a quest to understand more how native node modules are used. What I’m referring to, is using Node addons:

Addons are dynamically linked shared objects. They can provide glue to C and C++ libraries. The API (at the moment) is rather complex, involving knowledge of several libraries

Node.js statically compiles all its dependencies into the executable. When compiling your module, you don’t need to worry about linking to any of these libraries.

Since I maintain the Ionic CLI, we have a few depedencies to a native node module, node-sass.

Node-sass relies on some native C/C++ bindings to compile SASS down to CSS.

If you have a version of Node 0.12 for example, and install the ionic-cli, then install Node 4.2.1, you may have issues running the CLI.

This is due to the module building itself with that version of node and using those bindings, then when you install a new version of Node, you can’t use those same bindings.

When you change versions of node, make sure you do a quick rm -rf node_modules if on mac, or deleting the node_modules folder on windows and doing a fresh npm install.

If you want to read a little more, background information is shared by Chris Williams about his struggles maintaining the node-serialport module on this post.

Comments