electron-builder: Production build: unusable binary dependencies (Unexpected end of JSON input)

  • Version: v22.9.1
  • Electron Version: v11.0.0-beta.13
  • Electron Type (current, beta, nightly): beta
  • Target: macOS 10.15.2
  • Node Version: v12.16.1
  • Last Known Working Electron version: v10.1.4
  • Package manager version Yarn 1.22.10 (or NPM 6.13.4)

Hello,

We are having an issue when building our application with version 11-beta using electron-builder (the behavior changed between version 10.x and 11 of Electron). We rely on static binaries to be included in our build (ffmpeg, to name it), but when starting the built app we get a JS error saying the package.json file for this module is invalid (Unexpected end of JSON input). When looking at the content of the generated .app the (unpacked) app.asar has properly included the dependencies, and the package.json is valid.

The project runs fine on dev, it’s the finalized built version that has an issue. I created a repo with the base project, as well as uploaded the built version in the 1.0.0 release of this repo.

$ git clone https://github.com/lmancel/electron-tests.git
$ yarn install
$ yarn build

Built binary: https://github.com/lmancel/electron-tests/releases/download/1.0.0/electron-test-1.0.0-mac.zip

Screenshots

Screenshot 2020-10-21 at 16 41 49

Screenshot 2020-10-21 at 16 50 46

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 6
  • Comments: 17 (7 by maintainers)

Commits related to this issue

Most upvoted comments

It turned out not to be electron-builder related, see https://github.com/electron/electron/issues/26819.

I found a fix, exclude the packages with asarUnpack option:

 "asarUnpack": [
      "**/*.node",
      "**/node_modules/is-typings/**/*",
      "**/node_modules/@sindresorhus/do-not-disturb/**/*",
      "**/node_modules/@morajabi/robotjs/**/*",
...
]

and then from now on require these modules like this in your code:

require(require('path').join(__dirname.replace('app.asar', 'app.asar.unpacked'), 'node_modules/MODULE_NAME'))

(instead of require('MODULE_NAME'), note this does not work in dev, so you need to have an if statement)

If you use Webpack however, it’s simple, just use this in your webpack.config.js instead:

externals: {
   MODULE_NAME: IS_PROD
    ? `require(require('path').join(__dirname.replace('app.asar', 'app.asar.unpacked'), 'node_modules/${name}'))`
    : `commonjs2 ${name}`
}

(replace MODULE_NAME with names of the packages you excluded, for each of them you need to add a record to the externals config)

Verified that its fixed in electron 11.1.0! asar:false be gone!