-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
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
Cannot import external JSON in vite.config.ts on Node v22 #17291
Comments
Maybe duplicate of #14674, at least I noticed the same bug that vite incorrectly strips import assertions during compilation against a |
In my case, I feel the problem in processing vite.config.ts, not in the bundling of app itself. BTW, there seems to be no doubt that esbuild is erasing import attributes.
|
That looks like a bug in esbuild. They were initially introduced in node 21 but recently backported into 18 and 20. I opened evanw/esbuild#3778. |
For anyone having trouble: dynamic import is not converted by esbuild, so the following code will import JSON in Node v22 as well: const { default: meteocons } = await import(
"@iconify-json/meteocons/icons.json",
{
with: { type: "json" },
}
); |
Describe the bug
EDIT: I thought I was trying to switch between Node.js versions using Volta, but in fact when I called vite from pnpm it was fixed at Node v22.
I tried again with v20 and found no problem when using import assertions, so I have corrected the title.
Import assertions have been disabled since Node v22.0.0, so we can import JSON up to Node v20 or v21 with no problem if we use the conventional notation:
assert { type: "json" }
.I am trying to use
@iconify-json/meteocons
in my Vite plugin.I tried the following import methods, but none of them worked:
Those will fail with one of the following errors:
or
I believe that importing JSON in ESM style config files is no longer possible in Node.js v22 because Node.js v22 only supports import attributes instead of deprecated import assertions, and because esbuild removes import attributes under certain conditions (at least with
target: ["node18"]
).I have verified that by patching the following line to
target: ["node22"]
, it works correctly for both Node v20 and Node v22.vite/packages/vite/src/node/config.ts
Line 1077 in e7d38ab
Reproduction
https://github.com/SegaraRai/vite-config-import-json
Steps to reproduce
Run
pnpm i
followed bypnpm build
with Node.js v22.An error log is also available in GitHub Actions.
System Info
Used Package Manager
pnpm
Logs
Click to expand! - ERR_IMPORT_ATTRIBUTE_MISSING version
Click to expand! - SyntaxError version
Validations
The text was updated successfully, but these errors were encountered: