question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`import './file.json'`: This module is declared with using 'export ='

See original GitHub issue

In 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:

  1. 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:open
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
teppeiscommented, Jan 26, 2021

In ECMAScript, importing JSON will be implemented with JSON modules (stage 2) based on Import Assertions (stage 3).

import json from "./foo.json" assert { type: "json" };
import("foo.json", { assert: { type: "json" } });

In TypeScript: Implement Import Assertions (stage 3) · Issue #40694 · microsoft/TypeScript

3reactions
fregantecommented, May 17, 2020

I had to do some research:

  • The spec doesn’t seem to support loading JSON files yet, can’t find any mentions of it
  • Firefox 74 errors out with:

    Loading module from “./package.json” was blocked because of a disallowed MIME type (“application/json”)

  • Node 13 supports it experimentally

    When the --experimental-json-modules flag is included both the commonjs and module mode will use the new experimental JSON loader. The imported JSON only exposes a default, there is no support for named exports.

    import packageConfig from './package.json';
    
    Sadly the WHATWG link in Node’s docs is a gone from the spec
  • Rollup supports it with a plugin
    import pkg from './package.json';
    console.log(`running version ${pkg.version}`);
    
  • Webpack maps it to a require, so import list from './list.json works as expected
  • Browserify doesn’t support ES Modules, but I assume that the esm 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found