Repository navigation
Module resolution: Regression in JSON import using Node16 resolution #60589
Copy link
Copy link
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Milestone
Description
Demo Repo
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 here
> cd examples/chat-explorer
> npx tsc --showConfig
{
"compilerOptions": {
"composite": true,
"strict": true,
"declaration": true,
"module": "node16",
"target": "es2020",
"moduleResolution": "node16",
"allowSyntheticDefaultImports": true,
"useDefineForClassFields": true,
"skipLibCheck": true,
"rootDir": "./src",
"outDir": "./lib",
"lib": [
"es2020",
"dom",
"dom.iterable"
],
"jsx": "react-jsx",
"noUnusedLocals": true,
"noFallthroughCasesInSwitch": true,
"resolveJsonModule": true,
"moduleDetection": "force",
"esModuleInterop": true,
"resolvePackageJsonExports": true,
"resolvePackageJsonImports": true,
"incremental": true,
"noImplicitAny": true,
"noImplicitThis": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictBindCallApply": true,
"strictPropertyInitialization": true,
"strictBuiltinIteratorReturn": true,
"alwaysStrict": true,
"useUnknownInCatchVariables": true
},
"references": [
{
"path": "../../packages/core"
},
{
"path": "../../packages/chain-atp"
},
{
"path": "../../packages/chain-cosmos"
},
{
"path": "../../packages/chain-ethereum"
},
{
"path": "../../packages/chain-ethereum-viem"
},
{
"path": "../../packages/chain-solana"
},
{
"path": "../../packages/chain-substrate"
},
{
"path": "../../packages/signatures"
}
],
"files": [
"./src/ActionsTable.tsx",
"./src/HomePage.tsx",
"./src/NetworkPlot.tsx",
"./src/SessionsTable.tsx",
"./src/main.tsx",
"./src/useCursorStack.ts",
"./src/utils.ts",
"./src/vite-env.d.ts",
"./src/components/DidPopover.tsx",
"./src/components/Navbar.tsx",
"./src/components/PaginationButton.tsx"
],
"include": [
"src"
],
"exclude": [
"/Users/selkie/Development/canvas/examples/chat-explorer/lib"
]
}
Run tsc --traceResolution and paste its output here
File '/Users/selkie/Development/canvas/examples/chat-explorer/src/package.json' does not exist.
Found 'package.json' at '/Users/selkie/Development/canvas/examples/chat-explorer/package.json'.
======== Resolving module 'react/jsx-runtime' from '/Users/selkie/Development/canvas/examples/chat-explorer/src/ActionsTable.tsx'. ========
Explicitly specified module resolution kind: 'Node16'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
File '/Users/selkie/Development/canvas/examples/chat-explorer/src/package.json' does not exist according to earlier cached lookups.
File '/Users/selkie/Development/canvas/examples/chat-explorer/package.json' exists according to earlier cached lookups.
Loading module 'react/jsx-runtime' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory '/Users/selkie/Development/canvas/examples/chat-explorer/src/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/selkie/Development/canvas/examples/node_modules' does not exist, skipping all lookups in it.
Found 'package.json' at '/Users/selkie/Development/canvas/node_modules/react/package.json'.
Using 'exports' subpath './jsx-runtime' with target './jsx-runtime.js'.
File name '/Users/selkie/Development/canvas/node_modules/react/jsx-runtime.js' has a '.js' extension - stripping it.
File '/Users/selkie/Development/canvas/node_modules/react/jsx-runtime.ts' does not exist.
File '/Users/selkie/Development/canvas/node_modules/react/jsx-runtime.tsx' does not exist.
File '/Users/selkie/Development/canvas/node_modules/react/jsx-runtime.d.ts' does not exist.
Export specifier './jsx-runtime' does not exist in package.json scope at path '/Users/selkie/Development/canvas/node_modules/react'.
Found 'package.json' at '/Users/selkie/Development/canvas/node_modules/@types/react/package.json'.
Entering conditional exports.
Saw non-matching condition 'types@<=5.0'.
Matched 'exports' condition 'types'.
Entering conditional exports.
Matched 'exports' condition 'default'.
Using 'exports' subpath './jsx-runtime' with target './jsx-runtime.d.ts'.
File '/Users/selkie/Development/canvas/node_modules/@types/react/jsx-runtime.d.ts' exists - use it as a name resolution result.
'package.json' does not have a 'peerDependencies' field.
Resolved under condition 'default'.
Exiting conditional exports.
Resolved under condition 'types'.
Exiting conditional exports.
Resolving real path for '/Users/selkie/Development/canvas/node_modules/@types/react/jsx-runtime.d.ts', result '/Users/selkie/Development/canvas/node_modules/@types/react/jsx-runtime.d.ts'.
======== Module name 'react/jsx-runtime' was successfully resolved to '/Users/selkie/Development/canvas/node_modules/@types/react/jsx-runtime.d.ts' with Package ID '@types/react/jsx-runtime.d.ts@18.3.9'. ========
======== Resolving module 'swr' from '/Users/selkie/Development/canvas/examples/chat-explorer/src/ActionsTable.tsx'. ========
Explicitly specified module resolution kind: 'Node16'.
Resolving in ESM mode with conditions 'import', 'types', 'node'.
File '/Users/selkie/Development/canvas/examples/chat-explorer/src/package.json' does not exist according to earlier cached lookups.
File '/Users/selkie/Development/canvas/examples/chat-explorer/package.json' exists according to earlier cached lookups.
Loading module 'swr' from 'node_modules' folder, target file types: TypeScript, JavaScript, Declaration, JSON.
Searching all ancestor node_modules directories for preferred extensions: TypeScript, Declaration.
Directory '/Users/selkie/Development/canvas/examples/chat-explorer/src/node_modules' does not exist, skipping all lookups in it.
Directory '/Users/selkie/Development/canvas/examples/node_modules' does not exist, skipping all lookups in it.
Found 'package.json' at '/Users/selkie/Development/canvas/node_modules/swr/package.json'.
Entering conditional exports.
Saw non-matching condition 'react-server'.
Matched 'exports' condition 'import'.
Entering conditional exports.
Matched 'exports' condition 'types'.
Using 'exports' subpath '.' with target './dist/core/index.d.mts'.
File '/Users/selkie/Development/canvas/node_modules/swr/dist/core/index.d.mts' exists - use it as a name resolution result.
'package.json' has a 'peerDependencies' field.
Resolving real path for '/Users/selkie/Development/canvas/node_modules/swr', result '/Users/selkie/Development/canvas/node_modules/swr'.
File '/Users/selkie/Development/canvas/node_modules/react/package.json' exists according to earlier cached lookups.
Found peerDependency 'react' with '18.3.1' version.
Resolved under condition 'types'.
// ...
======== Module name 'vite' was successfully resolved to '/Users/selkie/Development/canvas/node_modules/vite/index.d.cts' with Package ID 'vite/index.d.cts@5.4.11+@types/node@20.14.10+lightningcss@1.28.1+terser@5.27.0'. ========
src/HomePage.tsx:1:22 - error TS1543: Importing a JSON file into an ECMAScript module requires a 'type: "json"' import attribute when 'module' is set to 'Node16'.
1 import * as pkg from "../package.json"
~~~~~~~~~~~~~~~~~
Found 1 error in src/HomePage.tsx:1
Paste the package.json of the importing module, if it exists
{
"name": "@canvas-js/chat-explorer",
"private": true,
"type": "module",
"version": "0.12.5",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"dev:server": "tsx --tsconfig server/tsconfig.json --watch server/server.ts",
"start:server": "cross-env NODE_ENV=production node dist/server.js"
},
"dependencies": {
"@canvas-js/chain-atp": "0.12.5",
"@canvas-js/chain-cosmos": "0.12.5",
"@canvas-js/chain-ethereum": "0.12.5",
"@canvas-js/chain-solana": "0.12.5",
"@canvas-js/chain-substrate": "0.12.5",
"@canvas-js/core": "0.12.5",
"@canvas-js/interfaces": "0.12.5",
"@ipld/dag-json": "^10.2.2",
"@radix-ui/themes": "^3.1.3",
"@vitejs/plugin-react": "^4.3.1",
"autoprefixer": "^10.4.20",
"cors": "^2.8.5",
"cross-env": "^7.0.3",
"d3": "^7.9.0",
"date-fns": "^3.6.0",
"express": "^4.19.2",
"express-ipld": "^0.0.1",
"http-status-codes": "^2.3.0",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"react-router-dom": "^6.20.1",
"swr": "^2.2.5",
"vite": "^5.4.8"
},
"devDependencies": {
"@types/cors": "^2.8.17",
"@types/d3": "^7.4.3",
"@types/express": "^4.17.21",
"@types/node": "^18.11.5",
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0"
}
}
Paste the package.json of the target module, if it exists
n/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#404
It 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
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptFix AvailableA PR has been opened for this issueA PR has been opened for this issue
Activity
kirkwaiblinger commentedon Nov 26, 2024
Concise repro:
(related, #60598)
jakebailey commentedon Nov 26, 2024
Andrew Branch (@andrewbranch)
jakebailey commentedon Nov 26, 2024
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?
raykyri commentedon Nov 26, 2024
That seems to be what's happening on our side, yes.
t3chguy commentedon Nov 27, 2024
I am seeing the same thing, and even if I bump the module to
nodenextthat does not help as import type assertions are forbidden onimport typestatementsandrewbranch commentedon Dec 3, 2024
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
--module node16, but I don’t think we want to suggest anyone use import assertions now. I guess we should just make #60019 have no effect onnode16, even thoughnode16seems to have been incorrect all along. Node.js v16 is EOL now, so I think we should deprecate--module node16and make a--module node18that enforces use of import attributes for JSON imports (likenodenextdoes now).kirkwaiblinger commentedon Dec 3, 2024
Andrew Branch (@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
*Nextsettings are more or less aliases of their latest concrete version.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
not having thought to exhaustively check node16 vs nodenext in addition to all the other relevant settings, would be that
*.nextversions of settings (including lib/target/module/modluleresolution ESNext/nodenext).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
module: Node18in a patch version is not enough fanfare to break through to users who may be wasting hours not knowing they're running into a node16/node18/nodenext discrepancy. Furthermore, there may be other undocumented node16 vs nodenext discrepancies that would have to be included in a stable node18 module version. I don't know.So I'm curious what the gameplan for actually fixing and communicating this issue out is.
kirkwaiblinger commentedon Dec 3, 2024
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 🙂):
andrewbranch commentedon Dec 3, 2024
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.
kirkwaiblinger commentedon Dec 3, 2024
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
andrewbranch commentedon Dec 3, 2024
I intend to patch
node16so it behaves the way it did in 5.6, while keepingnodenextthe way it is in 5.7.2. Docs do need to be changed (I just overlooked the import assertions/attributes difference when writing those docs originally). Beyond that, the strategy needs to be discussed with the team.andrewbranch commentedon Dec 3, 2024
Correction: I’d like to keep the “no named imports” part of #60019 in place for
node16in a patch, and only roll back the (unfulfillable) requirement of using import attributes/assertions.kirkwaiblinger commentedon Dec 3, 2024
Ok, this all makes sense. Thank you for the info!!
silverwind commentedon Dec 8, 2024
It seems odd that
nodenextandnode16are treated differently in 5.7 when the docs imply that they are aliases.I would have preferred to specify
node16to future-proof the code in casenodenextis updated to alias a newer node version, but this change around JSON imports now forces me to usenodenext.Edit: I saw #60673, which version does it apply to? 5.8?
kirkwaiblinger commentedon Dec 8, 2024
silverwind see also #60507.
andrewbranch commentedon Dec 9, 2024
silverwind #60673 will be released in a 5.7 patch soon. You can always use
typescript@nextin the meantime. Also, see #60705 for 5.8Change typescript `module` to `nodenext` (#32757)
Change typescript `module` to `nodenext` (#32757)