eleventy
eleventy copied to clipboard
Eleventy doesn't correctly resolve ESM (custom server)
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
Can you provide an minimum test case of your server module?
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.
Milestoning to 3.0
Maybe related to #3200
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',
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)
I can confirm that this issue has been fixed in 3.0.0-alpha.7. Thank you for your tireless commitment!
Excellent test case—thank you very much. This will ship fixed in 3.0.0-alpha.7 (or you can test via GitHub’s
mainbranch now)
Hey @zachleat, it seems that starting from Node.js 18.19.1+ and 11ty 3.0-alpha-8+ this issue appeared again
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?
Hey @VividVisions !
Here are the logs:
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 👍