'Why are native node modules always recompiled on npm install?

I am building an app with Electron 14, and node v14.17.7, respectively [npm 6.14.15] to build my native node modules.

Every time I execute npm install all my native dependencies are rebuilt from source (since the combination of Electron and node version is not available as a prebuilt in the repo).

Here is an exert from the logs:

• electron-builder  version=22.14.13
  • loaded configuration  file=package.json ("build" field)
  • rebuilding native dependencies  [email protected] platform=darwin arch=x64
  • install prebuilt binary  name=foo version=9.2.4 platform=darwin arch=x64 napi=
  • build native dependency from sources  name=foo
                                          version=9.2.4
                                          platform=darwin
                                          arch=x64
                                          napi=
                                          reason=prebuild-install failed with error
                                          (run with env DEBUG=electron-builder to get more information)
    prebuild-install WARN install prebuilt binaries enforced with --force!
    prebuild-install WARN install prebuilt binaries may be out of date!

The following message stands out:

WARN install prebuilt binaries enforced with --force!

The line is printed here

if (opts.force) {
  log.warn('install', 'prebuilt binaries enforced with --force!')
  log.warn('install', 'prebuilt binaries may be out of date!')

Unfortunately, I have no idea or clue where force is set to true. Can anyone help?

The build field of the package.json is this:

"build": {
    "appId": "com.foo.foo",
    "productName": "foo",
    "buildVersion": "1.0.0",
    "publish": {
      "provider": "s3",
      "bucket": "foo",
      "region": "foo",
      "endpoint": "https://foo.s3.amazonaws.com"
    },
    "afterPack": "./scripts/afterPack.js",
    "afterSign": "./scripts/notarization.js",
    "afterAllArtifactBuild": "./scripts/notarization_dmg.js",
    "files": [
      "dist/**/*",
      "main.js",
    ],
    "extraResources": [
      "./extra/**"
    ],
    "dmg": {
      "sign": true
    },
    "mac": {
      "binaries": [
        "./python34/bin/python3.4",
      ],
      "target": [
        "zip",
        "dmg"
      ],
      "hardenedRuntime": true,
      "entitlements": "./scripts/entitlements.mac.plist",
      "icon": "./public/icons/mac/icon.icns"
    },
    "directories": {
      "output": "foo-release",
      "buildResources": "public"
    }


Solution 1:[1]

Set DEBUG=electron-builder before running npm install to get more info.

Also consider this note from prebuild-install

Instead of prebuild paired with prebuild-install, we recommend prebuildify paired with node-gyp-build.

With prebuildify, all prebuilt binaries are shipped inside the package that is published to npm, which means there's no need for a separate download step like you find in prebuild. The irony of this approach is that it is faster to download all prebuilt binaries for every platform when they are bundled than it is to download a single prebuilt binary as an install script.

Upsides:

  1. No extra download step, making it more reliable and faster to install.
  2. Supports changing runtime versions locally and using the same install between Node.js and Electron. Reinstalling or rebuilding is not necessary, as all prebuilt binaries are in the npm tarball and the correct one is simply picked on runtime.
  3. The node-gyp-build runtime dependency is dependency-free and will remain so out of principle, because introducing dependencies would negate the shorter install time.
  4. Prebuilt binaries work even if npm install scripts are disabled. 5.The npm package checksum covers prebuilt binaries too.

Edit: Try

  • setting "npmRebuild": "false", in the build field (via)
  • setting opts.force = false at node_modules/prebuild-install/bin.js:45
  • npm install --verbose for even more output and
  • npm install --timing to save the debug logs for examination afterwards if the former gives too much.
  • Making a npm-dbg copy of npm with added --inspect-brk at the end between $NODE and $NPM_CLI_JS. Then use a Chromium browser's devtools/your IDE to step through the install process with hotkeys F10 and F11 in browsers. You can set breakpoints in advance or insert them via debugger; and the run 'till there to speedup this process with hotkey F8.
  • switching to prebuildify

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1