Question:
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?
Answer:
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.
- Find an OS image that resembles Amazon’s Linux distro (Fedora, CentOS, others that use
yum
as package manager – see Wiki) - 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) - For extra comfort, the script can also create the zip file for deployment
- Once the installation finishes, the script will shutdown the VM to avoid unnecessary consumption of system resources
- 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.