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

Eleventy doesn't correctly resolve ESM (custom server) #3178

Closed
VividVisions opened this issue Jan 29, 2024 · 10 comments
Closed

Eleventy doesn't correctly resolve ESM (custom server) #3178

VividVisions opened this issue Jan 29, 2024 · 10 comments
Labels
bug feature: esm Related to supporting ES Modules (in parallel to CommonJS)

Comments

@VividVisions
Copy link
Contributor

Operating system

macOS Ventura 13.6.3

Eleventy

3.0.0-alpha.4

Describe the bug

When I set my custom Eleventy server (written as ESM and linked via npm ln) like this:

eleventyConfig.setServerOptions({
   module: 'my-custom-server'
});

I get this error (I redacted my local paths):

[11ty] There was an error with your custom Eleventy server. We’re using the default server instead.
[11ty] Directory import '[…]/node_modules/my-custom-server' is not supported resolving ES modules imported from […]/node_modules/@11ty/eleventy/src/Util/Require.js
[11ty] Did you mean to import […]/my-custom-server/lib/server.js?

But if I use the proposed (and correct) path, I get this error:

[11ty] There was an error with your custom Eleventy server. We’re using the default server instead.
[11ty] "undefined" is not valid JSON

because Eleventy tries to open […]/node_modules/my-custom-server/lib/server.js/package.json.

Reproduction steps

See description.

Expected behavior

Eleventy should correctly resolve ES modules.

Reproduction URL

No response

Screenshots

No response

@zachleat
Copy link
Member

Can you provide an minimum test case of your server module?

@zachleat zachleat added needs-test-case Please submit a reproducible test case showcasing the issue! bug and removed needs-triage labels Feb 10, 2024
@VividVisions
Copy link
Contributor Author

Please take a look at https://github.com/VividVisions/11ty-test-3178.

npm run bsync loads the configuration file browsersync.eleventy.config.js which tries to swap back to Browsersync as per documentation.

npm run custom loads the configuration file custom.eleventy.config.js which tries to load a custom server (./lib/custom-server/, only extends EleventyDevServer in this case).

More infos in the README. Please let me know if you need anything.

@zachleat zachleat added the feature: esm Related to supporting ES Modules (in parallel to CommonJS) label Apr 10, 2024
@zachleat zachleat added this to the Eleventy 3.0.0 milestone Apr 10, 2024
@zachleat zachleat removed the needs-test-case Please submit a reproducible test case showcasing the issue! label Apr 10, 2024
@zachleat
Copy link
Member

Milestoning to 3.0

@zachleat
Copy link
Member

Maybe related to #3200

@VividVisions
Copy link
Contributor Author

I've updated my test case to use alpha.6 (and also fixed the config): https://github.com/VividVisions/11ty-test-3178
The underlying problem still seems to exist. The debug log shows the following:

Eleventy:ImportJsonSync Attempted to import '/[…]/11ty-test-3178/node_modules/custom-server/server.js/package.json',

@zachleat
Copy link
Member

zachleat commented Apr 17, 2024

Excellent test case—thank you very much. This will ship fixed in 3.0.0-alpha.7 (or you can test via GitHub’s main branch now)

@VividVisions
Copy link
Contributor Author

I can confirm that this issue has been fixed in 3.0.0-alpha.7. Thank you for your tireless commitment!

@what1s1ove
Copy link

Excellent test case—thank you very much. This will ship fixed in 3.0.0-alpha.7 (or you can test via GitHub’s main branch now)

Hey @zachleat, it seems that starting from Node.js 18.19.1+ and 11ty 3.0-alpha-8+ this issue appeared again

@VividVisions
Copy link
Contributor Author

Hey @zachleat, it seems that starting from Node.js 18.19.1+ and 11ty 3.0-alpha-8+ this issue appeared again

Hi @what1s1ove! I've updated my test case to use 3.0-alpha-10 and tested with Node.js 18.19.1 and 21.5.0 and got no errors.
What errors do you get?

@what1s1ove
Copy link

what1s1ove commented Jun 3, 2024

Hey @VividVisions !

Here are the logs:

image

As you can see, firstly I started the app with node v18.19.0 (npm v10.2.3) and there are no any errors.

But once I switched node to v18.19.1 (npm v10.2.4) or any higher version I got this error:

[11ty] Eleventy CLI Fatal Error: (more in DEBUG output)
[11ty] 1. Error in your Eleventy config file 'eleventy.config.js'. (via EleventyConfigError)
[11ty] 2. require() of ES Module /Users/vladyslavzubko/Documents/projects/whatislove.dev/apps/whatislove-dev/eleventy.config.js from /Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/src/Util/Require.js not supported.
[11ty] Instead change the require of eleventy.config.js in /Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/src/Util/Require.js to a dynamic import() which is available in all CommonJS modules. (via Error)
[11ty] 
[11ty] Original error stack trace: Error [ERR_REQUIRE_ESM]: require() of ES Module /Users/vladyslavzubko/Documents/projects/whatislove.dev/apps/whatislove-dev/eleventy.config.js from /Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/src/Util/Require.js not supported.
[11ty] Instead change the require of eleventy.config.js in /Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/src/Util/Require.js to a dynamic import() which is available in all CommonJS modules.
[11ty]     at requireLocal (/Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/src/Util/Require.js:6:10)
[11ty]     at TemplateConfig.requireLocalConfigFile (/Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/src/TemplateConfig.js:287:23)
[11ty]     at TemplateConfig.mergeConfig (/Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/src/TemplateConfig.js:334:28)
[11ty]     at TemplateConfig.getConfig (/Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/src/TemplateConfig.js:159:26)
[11ty]     at new Eleventy (/Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/src/Eleventy.js:109:39)
[11ty]     at Object.<anonymous> (/Users/vladyslavzubko/.npm/_npx/34c007b21a377b7f/node_modules/@11ty/eleventy/cmd.js:60:16)

UPD:

I just updated the npm to the latest version (v10.8.1) and the error has disappeared on 18.xx and 20.xx versions of the node. Thank you for this question 😁 It forced me to think deeper 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug feature: esm Related to supporting ES Modules (in parallel to CommonJS)
Projects
None yet
Development

No branches or pull requests

3 participants