Installing node.js packages for different architecture - 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?

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:
> bcrypt#1.0.3 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

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.

Related

How do I check that my npm package is installable on all the Node versions and platforms I want to support?

I publish an npm package that uses C++ code using nan. How can I double check that my package can be installed/imported on Linux, macOS and Windows with Node 14+ after I run npm publish?

How can I force node to download packages for a specific architecture?

Context on my setup:
I am using Mac M1 and am running windows through parallels.
When running npm install the default package that is being downloaded is ia32 architecture and as far as I know parallels does not support x64 windows however x64 programs can be run on the ia32 architecture with no issues.
The package I am trying to download is from a custom repository and has no ia32 package build.
I am aware of commands such as npm install --arch=x64 and this works great however I am trying to bootstrap using lerna and would like to override the downloading dependencies step in lerna to use x64 packages
Is there a way to force node to install x64 packages through modifying the package.json or through another means?

electron install error : Generated checksum for "electron-v2.0.2-win32-x64.zip" did not match expected checksum

I want to install latest electron version in my existing angular application.So I followed npm command to install it.
Command : npm i electron#latest
Expected Behavior
Electron should installed successfully.
Actual Behavior
So, Whenever I tried to install electron, after downloading zip file and SAMSUN.txt file , it is throwing below error....
Error: Generated checksum for "electron-v2.0.2-win32-x64.zip" did not match expected checksum.
at ChecksumMismatchError.ErrorWithFilename (E:\CityLawElectron\node_modules\electron-download\node_modules\sumchecker\build.js:41:124)
at new ChecksumMismatchError (E:\CityLawElectron\node_modules\electron-download\node_modules\sumchecker\build.js:56:133)
at Hash. (E:\CityLawElectron\node_modules\electron-download\node_modules\sumchecker\build.js:203:22)
at emitNone (events.js:106:13)
at Hash.emit (events.js:208:7)
at emitReadable_ (_stream_readable.js:513:10)
at emitReadable (_stream_readable.js:507:7)
at addChunk (_stream_readable.js:274:7)
at readableAddChunk (_stream_readable.js:250:11)
at Hash.Readable.push (_stream_readable.js:208:10)
Please note that I have tried to clear my npm cache using npm cache clean --force command and tried to install. but it didn't work.
I also tried to install electron globally using npm i -g electron#latest, but that approach also didn't work.
Please provide solution as soon as possible because I m really stuck at this problem.
Electron Version: 2.0.2
Operating System : windows
node version : 8.11.2
npm version : 6.1.0
Last known working Electron version:
We can solve this issue by using following steps (OS - Windows 10)
Delete "SHASUMS256.txt-x.x.xx" file from "C:\Users{{UserName}}.electron".
Delete Cache folder from "C:\Users{{UserName}}\AppData\Local\electron".
Delete electron from node_modules folder.
Then install electron globally and locally by using following commands -
npm init -y //Note: The -y flag provides default options automatically
npm install electron -g //install electron globally
npm install electron --save-dev --save-exact //install electron as devdependencies
If you are using typescript then install it as global using below command-
npm install -g typescript
Above steps may help you.

Installing scss-lint with npm without Python

I'm trying to get gulp-scss-lint to work on my machine, but having trouble doing so.
Context
I have a feeling specific versions and environment settings may be important to my issue, so let me list my specific context:
Windows Server 2012 R2 Standard
NPM 2.14.7
Node 4.2.2
Gulp 3.9.0 installed -g
Running from Powershell 4.0
Repro
Here's a repro of my problem:
mkdir gulpscsslint and cd gulpscsslint
npm init with all default answers
npm install gulp --save-dev
npm install gulp-scss-lint --save-dev
new-item -itemtype file gulpfile.js and enter:
var gulp = require('gulp'), lint = require('gulp-scss-lint');
gulp.task('default', [], function() {
return gulp.src('*.scss').pipe(lint());
});
new-item -itemtype file styles.scss
gulp
Result:
[08:48:50] Using gulpfile ~\experiments\gulpscsslint\gulpfile.js
[08:48:50] Starting 'default'...
[08:48:50] 'default' errored after 32 ms
[08:48:50] Error in plugin 'gulp-scss-lint'
Message:
Error code 1
Error: Command failed: C:\Windows\system32\cmd.exe /s /c "scss-lint 'styles.scss' --format=JSON"
'scss-lint' is not recognized as an internal or external command,
operable program or batch file.
Expected result was obviously actual output from the linter.
So then I continued:
npm install scss-lint --save-dev
But it fails with failNoPython, with this snippet from the output:
Can not download file from https://raw.githubusercontent.com/sass/node-sass-binaries/v2.1.1/win32-x64-node-4.2/binding.node
gyp ERR! configure error
gyp ERR! stack Error: Can't find Python executable "python", you can set the PYTHON env variable
Question
Is it really a hard requirement to have Python to install this Node package? Or can it somehow be installed without installing Python?
As a footnote, now that I've fully written this question I realize this might be more something of a Github issue, but if so I might need some help (learning how to) find(ing) out what package or tool is giving me trouble (gulp-scss-lint? scss-lint? npm? node-sass or some other underlying package?).
You can manually install the package by cloning or downloading it directly from it's github repository and placing it into your targeted location.
Note:
node-gyp is a cross-platform command-line tool written in Node.js for compiling native addon modules for Node.js. Link
This package requires Python to work. Hundreds of packages, including npm and npminstall, rely on node-gyp to compile. On windows, a person can install the package windows-build-tools, which includes python, to use node-gyp.

When installing locally (npm install .), can I have npm use my global packages?

When I run npm install . it takes a while to build packages that contain c code like expresso (which depends on node-jscoverage). I realized that I can copy expresso from my global package directory (~/Developer/lib/node_modules/expresso) to ./node_modules/expresso in my current directory before running npm install . and it won't bother compiling it. Is there a way to tell npm to try to install packages from my global npm directory before fetching and building them?
I guess this command might help: npm link
Check out this: npm to install packages from local position rather than from web?