`import './file.json'`: This module is declared with using 'export ='
See original GitHub issueIn short, JSON files’s implied type definition is always defined via export =
, even if I’m using module: es2015
, causing it to error unless I use an unnecessary flag (allowSyntheticDefaultImports
)
TypeScript Version: 3.9.0-dev.20200326
Search Terms:
import require json module commonjs es2015
Code
import list from "./list.json";
for (const item of list) {
console.log(item)
}
{
"compilerOptions": {
"outDir": "distribution",
"target": "es2017",
"module": "es2015",
"moduleResolution": "node",
"resolveJsonModule": true,
"strict": true
},
"files": [
"index.ts"
]
}
Expected behavior:
list
is of type string[]
Actual behavior:
index.ts:1:8 - error TS1259: Module '"./list"' can only be default-imported using the 'allowSyntheticDefaultImports' flag
1 import list from "./list.json";
~~~~
list.json:1:1
1 [
~
This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.
Found 1 error.
Workaround:
allowSyntheticDefaultImports: true
works as expected, but that flag is unsafe and I don’t want (need) to use it.
```ts
declare module './list.json' {
const list: string[];
export default list;
}
```
Playground Link: repro.zip
Related Issues: https://github.com/microsoft/TypeScript/issues/15146
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
This module is declared with using 'export =', and can only be ...
This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag ;...
Read more >How to Import JSON file as a Module - Bits and Pieces
Learn how to import JSON as a module using Import Assert, a TC39 Stage ... to import and export JavaScript functions and objects...
Read more >How to import JSON files in ES modules (Node.js) - Stefan Judis
Option 1: Read and parse JSON files yourself. The Node.js documentation advises to use the fs module and do the work of reading...
Read more >TSConfig Option: allowSyntheticDefaultImports - TypeScript
import * as React from "react";. When the module does not explicitly specify a default export. For example, without allowSyntheticDefaultImports as true:.
Read more >import - JavaScript - MDN Web Docs - Mozilla
In order to use the import declaration in a source file, the file must be interpreted by the runtime as a module. In...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
In ECMAScript, importing JSON will be implemented with JSON modules (stage 2) based on Import Assertions (stage 3).
In TypeScript: Implement Import Assertions (stage 3) · Issue #40694 · microsoft/TypeScript
I had to do some research:
require
, soimport list from './list.json
works as expectedesm
module does what Webpack does.Ultimately, I think that TypeScript’s behavior is correct since
import './file.json'
is technically invalid, even though experimentally supported in some places.The right flag to enable support (at the moment) seems to be
allowSyntheticDefaultImports
— it makes TS work and it generates invalid code, as expected.