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?
A package should be installed globally when it provides an executable command that you run from the shell (CLI), and it's reused across projects. You can also install executable commands locally and run them using npx, but some packages are just better installed globally.
With npm or yarn, you can install a package under a custom alias. This enables you to install multiple versions of a package in the same project.
Use npm list [package-name] to know the specific latest version of an installed package. Use npm install [package-name]@[version-number] to install an older version of a package. Prefix a version number with a caret (^) or a tilde (~) to specify to install the latest minor or patch version, respectively.
It is not required to install NPM for a node project as any javascript code can be executed by just typing “node myprogram. js”, but it is highly unpractical that you will be writing all code from scratch yourself, so you will be installing packages and that's where NPM comes in. It is a node package manager.
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 :)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With