Ricky Mo
Ricky Mo

Reputation: 7648

Electron forge throws '.../python3.8" links out of the package' error when making with asar enabled on Mac

I am trying to distribute an electron project. So I follow the official guide and use electron-forge. Here is my config.forge:

"config": {
    "forge": {
      "packagerConfig": {
        "asar":true,
        "ignore":[
          "^/[.].+$",
          "^/app/src$",
          "^.*/tsconfig([.].*)?[.]json",
          "^/angular[.]json",
          "^/frontend$",
          "^/build$"
        ]
      },
      "makers": [
        {
          "name": "@electron-forge/maker-squirrel",
          "config": {
            "name": "my-app"
          }
        },
        {
          "name": "@electron-forge/maker-zip",
          "platforms": [
            "darwin"
          ]
        },
        {
          "name": "@electron-forge/maker-deb",
          "config": {}
        },
        {
          "name": "@electron-forge/maker-rpm",
          "config": {}
        }
      ]
    }
  }

It builds fine without asar:true. But if I add asar:true, it throws this error:

An unhandled rejection has occurred inside Forge:
Error: /var/folders/k1/12r0xrxd01n7zgfpqfxppqm80000gn/T/electron-packager/darwin-arm64/my-app-darwin-arm64/Electron.app/Contents/Resources/app/node_modules/@serialport/bindings-cpp/build/node_gyp_bins/python3: file "../../../../../../../../../../../../../Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/bin/python3.8" links out of the package

Electron Forge was terminated. Location:
{}

Upvotes: 4

Views: 1897

Answers (2)

Will Munn
Will Munn

Reputation: 7947

A fix for this has been recently merged in node-gyp. While we wait for this to be upgraded there is a workaround, if you're using electron forge, you can remove the node_gyp_bins directory in a hook in your electron forge config. Something like this worked for me (replacing module name with the offending module).

const path = require('path')
const fs = require('node:fs/promises');
{
....
hooks: {
     packageAfterPrune: async (_config, buildPath) => {
       const gypPath = path.join(
         buildPath,
         'node_modules',
         'moduleName',
         'build',
         'node_gyp_bins'
       );
       await fs.rm(gypPath, {recursive: true, force: true});
    }
  }
}

Upvotes: 4

Tim Perry
Tim Perry

Reputation: 13216

This appears to be caused by this node-gyp issue: https://github.com/nodejs/node-gyp/issues/2713

Node-Gyp now creates links to the detect binary versions, apparently to help it consistently use the same versions during builds. These are left after the build finishes, and they point outside the project directory (e.g. to /usr/bin/python). That makes various tools refuse to bundle your node_modules with these links present.

This is really a bug in node-gyp imo, but fixing that may take some time. You could attempt to contribute a fix for that bug in the node-gyp project yourself, or in the meantime you can work around this by deleting all node_gyp_bins folders in your node_modules before building.

Upvotes: 1

Related Questions