Cross-compile node module with native bindings with node-gyp


I’m using AWS Lambda, which involves creating an archive of my node.js script, including the node_modules folder and uploading that to their infrastructure to run.

This works fine, except when it comes to node modules with native bindings (using node-gyp). Because the binding was complied and project archived on my local computer (OS X), it is not compatible with AWS’s (Amazon Linux) servers.

How can I cross-compile/install a node module (specifically, node-sqlite3) so when I upload it to another server arch it runs?


While not really a solution to your problem, a very easy workaround could be to simply compile the native addons on a Linux machine.

For your particular situation, I would use Vagrant. Vagrant can create virtual machines and configure them within seconds.

  1. Find an OS image that resembles Amazon’s Linux distro (Fedora, CentOS, others that use yum as package manager – see Wiki)
  2. Use a simple configuration script that, when run by Vagrant on machine startup, will run npm install (optionally it might also remove the node_modules folder before to ensure a clean installation)
  3. For extra comfort, the script can also create the zip file for deployment
  4. Once the installation finishes, the script will shutdown the VM to avoid unnecessary consumption of system resources
  5. Deploy!

It might require some tuning if the linked libraries are not at the same place on the target machine but generally this seems to me like the best and quickest solution.

Leave a Reply