microsoft / microsoft/TypeScript

Dynamically importing JSON should require import attribute with node16/nodenext

Open
#60,598 1 comment 0 reactions 1 assignee View on GitHub

@andrewbranch is already working on this.

Since Nov 28, 2024.

Needs Investigation
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

🔎 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())).

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.