-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Module resolution: Regression in JSON import using Node16 resolution #60589
Comments
Concise repro: // "module": "node16", "resolveJsonModule": true, in a *.mts file (does not apply to cjs output)
// TS error: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'Node16'.ts(1543)
import something1 from './someThing.json';
// TS error: Import attributes are only supported when the '--module' option is set to 'esnext', 'nodenext', or 'preserve'.ts(2823)
import something2 from './someThing.json' with { type: "json" }; (related, #60598) |
The issue is pretty poorly formatted, so to restate, the problem is that we require an import attribute for JSON in Node16 resolution, but import attributes are not allowed in Node16 resolution? |
That seems to be what's happening on our side, yes. |
I am seeing the same thing, and even if I bump the module to
|
In Node.js 16, JSON files can’t be imported without an import assertion, and import attributes aren’t supported until 18 (I’m ignoring odd-numbered versions). It seems like it was a bug/oversight to allow JSON imports without assertions in |
@andrewbranch In principle, this resolution makes sense to me. However, I think the docs are problematic here. I have it ingrained into my head, and I'm guessing that many users do as well, that that the The TS docs have this:
...and this:
It seems like nodenext is already a nontrivial departure from node16 in undocumented ways. My request as a user, having spent many hours tracking down issues of which this issue and #60598, are the root causes, and
The reason I say this here, rather than immediately spinning off a new issue, is that I note that this is marked for TS 5.7.3, and I worry that subtly dropping So I'm curious what the gameplan for actually fixing and communicating this issue out is. |
To be concrete, it seems to be that this can (and should) be done in a patch change, 👍
but I would add that I think that import attributes also should be allowed in node16, since otherwise users of >=node18 can only use the "unstable" nodenext. It's this part that I'm worried about dropping in a patch version (even though, as a user, I do want it ASAP 🙂):
|
No option will ever be dropped in a patch version. In fact, our current policy is that no option ever be dropped without a deprecation, and nothing ever be deprecated without a major version bump, which occurs once every 2.5 years. |
Great! So, would the thought be that a patch include
and the next minor include
Sorry for pestering on this, but the expected resolution here informs some decision-making for me (on a node18 project that also needs to be upgraded from 'commonjs' to 'node16'/'nodenext') on whether to move it to
|
I intend to patch |
Correction: I’d like to keep the “no named imports” part of #60019 in place for |
Ok, this all makes sense. Thank you for the info!! |
It seems odd that I would have preferred to specify Edit: I saw #60673, which version does it apply to? 5.8? |
@silverwind see also #60507. |
@silverwind #60673 will be released in a 5.7 patch soon. You can always use |
Typescript 5.7 changed semantics around JSON imports and `nodenext` is now [treated differently](https://devblogs.microsoft.com/typescript/announcing-typescript-5-7-beta/#validated-json-imports-in---module-nodenext) than `node16` for JSON imports and it requires the import attribute, so change the value to that and add the attribute to eliminate this typescript error. [`moduleResolution`](https://www.typescriptlang.org/tsconfig/#moduleResolution) is treated as an alias when `module` is `nodenext`, so we don't need to specify it. Also see microsoft/TypeScript#60589. It appears the next Typescript release will fix this for `node16`, but I guess it'll still be good to switch to `nodenext`.
Demo Repo
canvasxyz/canvas#405
Which of the following problems are you reporting?
Something else more complicated which I'll explain in more detail
Demonstrate the defect described above with a code sample.
import * as pkg from "../package.json"
Run
tsc --showConfig
and paste its output hereRun
tsc --traceResolution
and paste its output herePaste the
package.json
of the importing module, if it existsPaste the
package.json
of the target module, if it existsn/a
Any other comments can go here
A recent change that landed around 5.7.0 seems to break Node16 imports of .json files, because a
type: json
import attributed is required but also not allowed to be used: canvasxyz/canvas#404It might be related to this issue: #59684
Which was addressed in this PR: #60019
It seems to produce new errors in these automated builds: https://github.com/search?q=repo%3Amicrosoft%2FTypeScript+%22named+imports+are+not+allowed%22&type=issues
The text was updated successfully, but these errors were encountered: