Installing node.js packages for different architecture

I need to install npm packages that are for a different target architecture (Linux x64) than the machine I am running npm from (Windows x86). Is there a way to tell npm install to download packages that are for the other OS/architecture?


Solution 1:

Most native node modules use node-pre-gyp which uses an install script to search for pre-built binaries for your OS/arch/v8 ABI combination, and fallback to native build if one is not available.

Assuming your native modules use node-pre-gyp, you can do this:

npm i --target_arch=x64 --target_platform=linux

You'll see something like this in the output:

> [email protected] install /home/user/myProject/node_modules/bcrypt
> node-pre-gyp install --fallback-to-build

[bcrypt] Success: "/home/user/myProject/node_modules/bcrypt/lib/binding/bcrypt_lib.node" is installed via remote

If it can't find a prebuilt binary, node-pre-gyp will fall back to attempting to build the module from source.

If the prebuilt modules aren't downloadable, there's also a way to build & host them from your own mirror, but that's a different question :)

Solution 2:

Most binary npm packages compile the .node binary from source. You can't really force cross-compilation with npm, but you can possibly create a postinstall script to recompile the particular dependency that re-runs node-gyp with an --arch flag:

"postinstall" : "node-gyp -C node_modules/your-dependency clean configure --arch=x86_64 rebuild"

You will need a proper compiler toolchain. I'm sot sure what it is for windows, but probably you'll end up using mingw or cygwin