Specify target node version while building node extension
Node.JS is marvellous when it comes to extending its functionality using modules. There are thousands of addon modules available for Node easing the development. Apart from that, Node.JS allows us to write C++ (yes! C++) addons to extend its functionality beyond pure JS. In case we need to perform some CPU intensive task and want to leverage the full power of underlying CPU, C++ is the obvious choice. Node.JS had thus made it feasible for a JS code to utilize this using C++ Addons.
While compiling C++ Addon (or extension/native addon) using the node-gyp build system, sometimes we need to specify the exact node version, else the build fails since the extension we are building needs header files specific to a particular version. This issue occurs when we have a node.js version installed on the system which differs from the target node version. Even though the fix is relatively very simple, this causes an unnecessary head-ache while debugging the build issue.
All we need to do to solve this is to specify the node version in the node-gyp configure command as follows:
node-gyp --arch=<WHATEVER> --target=<TargetVersion> configure
e.g: node-gyp –arch=x86 –target=v0.8.20 configure
That’s all you need to tell the build system that which node.js version you want to build the extension with!
I hope this helps!