eleventy: Import attributes not yet supported (Error thrown from `acorn` dependency)
Operating system
macOS Sonoma 14.2
Eleventy
3.0.0-alpha.2
Describe the bug
Hello from a courageous canary tester! 👋
I have upgraded to 3.0.0-alpha.2, and converted my project from CommonJS to ESM. The site builds without error (npx eleventy), but when serving (npx eleventy --serve) the following error is thrown:
[11ty] Unexpected token (9:33) (via SyntaxError)
[11ty]
[11ty] Original error stack trace: SyntaxError: Unexpected token (9:33)
[11ty] at pp$4.raise (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/acorn/dist/acorn.js:3571:15)
[11ty] at pp$9.unexpected (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/acorn/dist/acorn.js:772:10)
[11ty] at pp$9.semicolon (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/acorn/dist/acorn.js:749:68)
[11ty] at pp$8.parseImport (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/acorn/dist/acorn.js:1845:10)
[11ty] at pp$8.parseStatement (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/acorn/dist/acorn.js:948:51)
[11ty] at pp$8.parseTopLevel (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/acorn/dist/acorn.js:829:23)
[11ty] at Parser.parse (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/acorn/dist/acorn.js:601:17)
[11ty] at Function.parse (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/acorn/dist/acorn.js:651:37)
[11ty] at Object.parse (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/acorn/dist/acorn.js:5960:19)
[11ty] at findByContents (/Users/paulrobertlloyd/Sites/paulrobertlloyd-v4/node_modules/@11ty/dependency-tree-esm/main.js:40:18)
I am using 11ty’s default dev server.
Branch: https://github.com/paulrobertlloyd/paulrobertlloyd-v4/tree/eleventy-esm Upgrade commit: https://github.com/paulrobertlloyd/paulrobertlloyd-v4/commit/ba4cd6461ad31351040a040498bc91661bae7f99
Reproduction steps
No response
Expected behavior
No response
Reproduction URL
No response
Screenshots
No response
About this issue
- Original URL
- State: closed
- Created 3 years ago
- Reactions: 3
- Comments: 16 (8 by maintainers)
Commits related to this issue
- Improved error messaging for #3128 — committed to 11ty/eleventy by zachleat 2 years ago
- Import attributes fix https://github.com/11ty/eleventy/issues/3128#issuecomment-2528475323 — committed to eeeps/baabo by eeeps 2 years ago
Improved the error messaging here in 3.0.0-alpha.14 after looking at an issue from @bobmonsour’s 11ty bundle (https://github.com/bobmonsour/11tybundle.dev/blob/839b73059e3efce10369829125d8ebc5dba89fdc/src/_data/bundledata.js) (cc @uncenter too):
The awkward part of this issue is that Node.js will parse these fine (with an experimental warning) but the
acorndependency fails with an error. So your build will succeed without issue but the--watchor--servestep post-build will fail.You can make a
require()to import JSON data like you would in CJS.And then you can just import JSON data using it like so:
Hello guys! Yes, the issue lies specifically with
import-attributes. Acorn only supports the syntax that is part of ECMAScript. Sinceimport-attributesare still not part of ECMAScript (this proposal at stage 3), acorn doesn’t understand this syntax.https://github.com/acornjs/acorn/issues/1228#issuecomment-1670192079 (the issue is about decorators, but this proposal is also on the stage 3 right now)
Going to track this one here to circle back when Import Attributes are stable in Node.js (and included a summary of this thread as well!): https://github.com/11ty/eleventy-dependency-tree-esm/issues/2
Thanks y’all!
Thanks @what1s1ove; your solution earlier in this thread works, so I’ll use that for now. Thanks for the background info 🙏
If you upgrade to the latest version v3.1.0, you don’t need to use that workaround anymore - import attributes are natively supported in v3.1.0!
Fix for this will be included with Eleventy v3.0.1-alpha.1, release tracking issue #3580
Interesting, thanks for the background info!
I’m only using it to get the 11ty version used for building the site so I guess I can work around it somehow until it is fixed upstream.
The error seems unrelated to the
package.jsonimport.I get the error as well yet the building works just fine. 🤷
Ah, I’m using Node v20 for which
typeis now used instead ofassert. I updated my import to use the revised syntax, yet I still get the same error (when serving, not building).@what1s1ove You may need to import like so
That’s what solved the JSON import for me. In your case I guess you’d do
The
with { type: 'json' }tells node to parse this file in JSON and return a JSON object.Edit: Formatting
I had this issue when I tried to import a
jsonfile via Import attributes (https://nodejs.org/docs/latest-v18.x/api/esm.html#import-attributes). I just changed it toBut definitely want to return to Import attributes soon 🙏
I’m seeing the same issue on MacOS.