electron-userland / electron-builder

A complete solution to package and build a ready for distribution Electron app with “auto update” support out of the box
https://www.electron.build
MIT License
13.58k stars 1.73k forks source link

require() of ES Module not supported #7935

Closed lutzroeder closed 9 months ago

lutzroeder commented 9 months ago
  1. Set package.json to type: module.
  2. Create afterSign: notarize.js using ESM imports.
  3. Run npx electron-builder --mac --universal --publish never.
⨯ require() of ES Module ~/Projects/Test/notarize.js from ~/Projects/Test/node_modules/app-builder-lib/out/platformPackager.js not supported.
Instead change the require of notarize.js in ~/Projects/Test/node_modules/app-builder-lib/out/platformPackager.js to a dynamic import() which is available in all CommonJS modules.
mmaietta commented 9 months ago

Released in 24.10.0

omarabdula commented 3 months ago

@lutzroeder HELLO DID YOU MANAGE TO FIND A SOLUTION?

briantolsen commented 3 months ago

Running into this issue now as well using 24.13.3

mmaietta commented 3 months ago

So the current code detects if the package.json is of type module, but it automatically has a fallback of require from the try-catch logic https://github.com/electron-userland/electron-builder/blob/29f6504e181cef2c43ac070a80f24ee79dc52481/packages/app-builder-lib/src/platformPackager.ts#L775-L787

It should be fully functional for handling both ES and CJS modules in the next alpha version, would you mind giving it a try?

leo4life2 commented 2 months ago

Running into this issue now as well using 24.13.3

added some debug logs to platformPackager.js today, and turns out that the try part failed not because it's not an es module, but because there's bugs in my notarize.js's code, so it threw exceptions on import. for me it was because i gave bad arguments to the notarize function. @mmaietta perhaps the catch block could be tweaked a bit to check the error cause so the error message is more accurate?

Here's my working afterSign script:

import { notarize } from '@electron/notarize';

export default async function afterSign(context) {
  const { appOutDir, electronPlatformName } = context;

  if (electronPlatformName !== 'darwin') {
    return;
  }

  const appName = context.packager.appInfo.productFilename;

  await notarize({
    appPath: `${appOutDir}/${appName}.app`,
    appleApiKey: '~/private_keys/notarize.p8', // Path to your .p8 file
    appleApiKeyId: process.env.API_KEY_ID,
    appleApiIssuer: process.env.API_KEY_ISSUER_ID,
  });
}
mmaietta commented 1 month ago

@leo4life2 that's a great callout and something I'll implement.

Do you recall what the error message you were receiving? Was it Unable to dynamically import hook, falling back to "require" or an error being thrown during the fallback require statement?

mmaietta commented 1 month ago

Added better logging in https://github.com/electron-userland/electron-builder/pull/8356