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

Created on 22 Oct 2020  路  14Comments  路  Source: electron-userland/electron-builder

  • 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

Most helpful comment

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

All 14 comments

I tried to solve this by:

  1. get deps by
npx npm-remote-ls --flatten electron-settings -d false -o false
  1. asarUnpack all deps
config: {
    appId: 'com.tiddlygit.app',
    productName: 'TiddlyGit',
    asar: true,
    files: ['!tests/**/*', '!docs/**/*', '!template/**/*', '!flow-typed/**/*', '!localization/**/*'],
    extraFiles: [
      {
        from: 'template/wiki',
        to: 'wiki',
        filter: ['**/*'],
      },
      {
        from: 'localization',
        to: 'localization',
        filter: ['**/*'],
      },
    ],
    // add dependencies of tiddlywiki here
    // https://github.com/electron/electron/issues/18540#issuecomment-660679649
    asarUnpack: [
      // tiddlywiki in the worker_thread
      '**/node_modules/@tiddlygit/tiddlywiki/**/*',
      // dep of dugite, asar unpack it so we can solve https://github.com/desktop/dugite/issues/414
      '**/node_modules/dugite/**/*',
      '**/node_modules/rimraf/**/*',
      '**/node_modules/progress/**/*',
      '**/node_modules/mkdirp/**/*',
      '**/node_modules/minimist/**/*',
      '**/node_modules/glob/**/*',
      '**/node_modules/checksum/**/*',
      '**/node_modules/got/**/*',
      '**/node_modules/tar/**/*',
      '**/node_modules/fs.realpath/**/*',
      '**/node_modules/inflight/**/*',
      '**/node_modules/path-is-absolute/**/*',
      '**/node_modules/optimist/**/*',
      '**/node_modules/minimatch/**/*',
      '**/node_modules/inherits/**/*',
      '**/node_modules/@sindresorhus/is/**/*',
      '**/node_modules/@szmarczak/http-timer/**/*',
      '**/node_modules/decompress-response@/**/*',
      '**/node_modules/duplexer3/**/*',
      '**/node_modules/cacheable-request/**/*',
      '**/node_modules/get-stream/**/*',
      '**/node_modules/lowercase-keys/**/*',
      '**/node_modules/mimic-response/**/*',
      '**/node_modules/p-cancelable/**/*',
      '**/node_modules/to-readable-stream/**/*',
      '**/node_modules/url-parse-lax/**/*',
      '**/node_modules/fs-minipass/**/*',
      '**/node_modules/chownr/**/*',
      '**/node_modules/safe-buffer/**/*',
      '**/node_modules/once/**/*',
      '**/node_modules/minizlib/**/*',
      '**/node_modules/wrappy/**/*',
      '**/node_modules/wordwrap/**/*',
      '**/node_modules/defer-to-connect/**/*',
      '**/node_modules/minipass/**/*',
      '**/node_modules/clone-response/**/*',
      '**/node_modules/get-stream/**/*',
      '**/node_modules/http-cache-semantics/**/*',
      '**/node_modules/keyv/**/*',
      '**/node_modules/lowercase-keys/**/*',
      '**/node_modules/brace-expansion/**/*',
      '**/node_modules/responselike/**/*',
      '**/node_modules/pump/**/*',
      '**/node_modules/normalize-url/**/*',
      '**/node_modules/yallist/**/*',
      '**/node_modules/json-buffer/**/*',
      '**/node_modules/balanced-match/**/*',
      '**/node_modules/concat-map/**/*',
      '**/node_modules/prepend-http/**/*',
      '**/node_modules/end-of-stream/**/*',
      // deps of electron-settings
      '**/node_modules/electron-settings/**/*',
      '**/node_modules/lodash.get/**/*',
      '**/node_modules/lodash.set/**/*',
      '**/node_modules/write-file-atomic/**/*',
      '**/node_modules/electron-settings/node_modules/mkdirp/**/*',
      '**/node_modules/lodash.has/**/*',
      '**/node_modules/lodash.unset/**/*',
      '**/node_modules/is-typedarray/**/*',
      '**/node_modules/signal-exit/**/*',
      '**/node_modules/imurmurhash/**/*',
      '**/node_modules/typedarray-to-buffer/**/*'
    ],
    extraResources: [

This works for dugite, but not work for electron-settings, though I add it and all its deps to asarUnpack, it still said it is inside asar.

Screen Shot 2020-11-01 at 11 09 45 AM

try asar: false solves this, but will impact start-up time.

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)

@Momakes

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)

Thank you for this. Is this method possible in typescript?

The workaround is to set asar: false in electron-builder config (another one is to explicitly repack the asar but this is a bad one). The option listed by @momakes2 looks unnecessary hacky.

I agree @vladimiry. Do you know if a fix is planned soon?

Thanks Vlad.. thought as much. We may just disable asar until fixed then. Thanks again

Do you know if a fix is planned soon?

I've not seen any signs of the fix to be planned. Two related issues placed in https://github.com/electron/electron/issues been closed without resolution as irrelevant.

This is happening with electron 11.0.3. As far as I can tell, it's not possible to use electron 11 with electron-builder at the moment.

As far as I can tell, it's not possible to use electron 11 with electron-builder at the moment.

asar: false

asar is recommended both by electron and electron-builder, so I don't think that's really a solution IMO.

Of course disabling asar packing is not a solution but a workaround.

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

thank you for your efforts @vladimiry

Was this page helpful?
0 / 5 - 0 ratings