Skip to content

Module resolution: Regression in JSON import using Node16 resolution #60589

Closed
@raykyri

Description

@raykyri

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 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/[email protected]'. ========
======== 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/[email protected]+@types/[email protected][email protected][email protected]'. ========
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

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions