skb
skb

Reputation: 31104

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?

Upvotes: 32

Views: 35829

Answers (4)

Mathix420
Mathix420

Reputation: 1287

Since ~2022 it seems that there are new flags to install architecture/os specific packages using npm.

--cpu and --os, both links to their documentation.

They are both linked to package.json os and cpu parameters.

Example:

npm i --cpu arm64 --os darwin

Upvotes: 8

cachius
cachius

Reputation: 1906

In case the package is electron, you can by

npm install --arch=x64 electron

or

export npm_config_arch=x64
npm install --arch=x64 electron

as described on https://www.electronjs.org/docs/latest/tutorial/installation. These are options of the electron-download package which downloads the actual binary. So they will work only for electron.

Upvotes: 3

thom_nic
thom_nic

Reputation: 8143

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 :)

Upvotes: 18

lxe
lxe

Reputation: 7599

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

Upvotes: 6

Related Questions