Skip to content

Dynamically importing JSON should require import attribute with node16/nodenextΒ #60598

Open
@kirkwaiblinger

Description

@kirkwaiblinger

πŸ”Ž Search Terms

dynamic import, import attribute, resolveJsonModule,

πŸ•— Version & Regression Information

⏯ Playground Link

https://github.com/kirkwaiblinger/repro-TS-dynamic-json-import-validation

πŸ’» Code

// module: node16/nodenext, resolveJsonModule
// .cts or .mts, it doesn't matter.

async function main() {
    const somethingDynamic = await import('./someThing.json');
    console.log('dynamically imported JSON:', somethingDynamic);
}

main();

export {}

πŸ™ Actual behavior

No error, even though this is a runtime error in nodejs.

πŸ™‚ Expected behavior

Error because dynamic import('./someThing.json') requires import attribute.

Additional information about the issue

Unlike the static import case, this is the case for both commonjs and esm outputs, since import() is available in commonjs modules in node and has the same semantics there as the ESM import (and therefore TS doesn't transform the import() statement to a Promise.resolve(require())).

Metadata

Metadata

Assignees

Labels

Fix AvailableA PR has been opened for this issueNeeds InvestigationThis issue needs a team member to investigate its status.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions