eleventy icon indicating copy to clipboard operation
eleventy copied to clipboard

Eleventy doesn't correctly resolve ESM (custom server)

Open VividVisions opened this issue 1 year ago • 1 comments

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

VividVisions avatar Jan 29 '24 13:01 VividVisions

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

zachleat avatar Feb 10 '24 16:02 zachleat

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.

VividVisions avatar Mar 13 '24 16:03 VividVisions

Milestoning to 3.0

zachleat avatar Apr 10 '24 14:04 zachleat

Maybe related to #3200

zachleat avatar Apr 11 '24 14:04 zachleat

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',

VividVisions avatar Apr 16 '24 10:04 VividVisions

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)

zachleat avatar Apr 17 '24 14:04 zachleat

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

VividVisions avatar Apr 25 '24 14:04 VividVisions

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

what1s1ove avatar May 24 '24 08:05 what1s1ove

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?

VividVisions avatar Jun 03 '24 10:06 VividVisions

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 👍

what1s1ove avatar Jun 03 '24 12:06 what1s1ove