Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing node.js packages for different architecture

Tags:

npm

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?

like image 675
skb Avatar asked Jul 25 '14 17:07

skb


People also ask

Should I install node packages globally?

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.

Does npm install multiple versions of same package?

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.

How do I install a specific version of node package?

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.

Do I need to install node for every project?

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.


2 Answers

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

like image 161
thom_nic Avatar answered Nov 30 '22 23:11

thom_nic


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

like image 27
lxe Avatar answered Dec 01 '22 00:12

lxe