Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ES module resolution fails when using main entrypoint in package.json #8036

Closed
nerdynomadd opened this issue Feb 6, 2024 · 2 comments · Fixed by #8042
Closed

ES module resolution fails when using main entrypoint in package.json #8036

nerdynomadd opened this issue Feb 6, 2024 · 2 comments · Fixed by #8042

Comments

@nerdynomadd
Copy link

  • Electron-Builder Version: 4.10.0
  • Node Version: 18.8.0
  • Electron Version: 28.0.0
  • Electron Type (current, beta, nightly): current
  • Target: macOS, windows

ES module resolution fails when "type": "module" is set into the package.json and the resolution is being done only depending on "main" field into the package.json

Here's my package.json:

{
    ...
    "type": "module",
    "main": "dist/main/main.js"
}

And here's the error I get from electron-builder when packaging the app:

⨯ require() of ES Module /Users/**/**/**/desktop-application/dist/main/main.js from /Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/platformPackager.js not supported.
Instead change the require of main.js in /Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/platformPackager.js to a dynamic import() which is available in all CommonJS modules.  failedTask=build stackTrace=Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/**/**/**/desktop-application/dist/main/main.js from /Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/platformPackager.js not supported.
Instead change the require of main.js in /Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/platformPackager.js to a dynamic import() which is available in all CommonJS modules.
    at resolveModule (/Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/platformPackager.js:596:17)
    at resolveFunction (/Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/platformPackager.js:614:21)
    at MacPackager.doSignAfterPack (/Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/platformPackager.js:253:33)
    at async MacPackager.doPack (/Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/platformPackager.js:238:13)
    at async MacPackager.pack (/Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/macPackager.js:157:17)
    at async Packager.doBuild (/Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/packager.js:360:17)
    at async executeFinally (/Users/**/**/**/node_modules/.pnpm/builder-util@24.9.4/node_modules/builder-util/out/promise.js:14:18)
    at async Packager._build (/Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/packager.js:306:35)
    at async Packager.build (/Users/**/**/**/node_modules/.pnpm/app-builder-lib@24.10.0/node_modules/app-builder-lib/out/packager.js:277:16)
    at async executeFinally (/Users/**/**/**/node_modules/.pnpm/builder-util@24.9.4/node_modules/builder-util/out/promise.js:14:18)

After a little bit of debugging into the platformPackager.js part in the function "resolveModule(type, name)", here's the output:

includeName /Users/**/**/**/**/apps/desktop-application
isModuleType true
moduleType module

Which means that the if condition will never be executed properly and that it defaults to the commonjs require instead of evaluating the dynamic import added recently in the following pull request

@nerdynomadd nerdynomadd changed the title ES module resolution fails when using "main" entrypoint in package.json ES module resolution fails when using main entrypoint in package.json Feb 6, 2024
@mmaietta
Copy link
Collaborator

mmaietta commented Feb 8, 2024

Just to confirm, are your electron-builder hooks written as CJS and not as modules?

@nerdynomadd
Copy link
Author

Yes, these files are located in a specific "scripts" directory and their extension is .cjs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants