Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

import fails with 'no default export' #8

Open
StokeMasterJack opened this issue May 12, 2017 · 24 comments
Open

import fails with 'no default export' #8

StokeMasterJack opened this issue May 12, 2017 · 24 comments

Comments

@StokeMasterJack
Copy link

This import works fine under babel:

import React from "react";

But under TypeScript it complains of:
(1,8): error TS1192: Module '"/Volumes/repos/tc-web-ts/node_modules/@types/react/index"' has no default export.

@snerks
Copy link

snerks commented May 13, 2017

Have you tried:

import * as React from 'react';

@StokeMasterJack
Copy link
Author

Yes. But I have hundreds of files like this. I thought TypeScript was a superset of JavaScript?

@bluetech
Copy link

Add "allowSyntheticDefaultImports": true to your tsconfig.json if you want it to work like this.

@nitishdayal
Copy link

^ + IME change tsconfig.json > compilerOptions.module to 'es2015'. That's the configuration I've been using on my React/TS projects, allows for the usual default import syntax that most have become accustomed to.

@StokeMasterJack
Copy link
Author

StokeMasterJack commented May 14, 2017

Add "allowSyntheticDefaultImports": true to your tsconfig.json if you want it to work like this.

This did not work. Now I am getting this error:

Uncaught TypeError: Cannot read property 'render' of undefined

Here is the entire app:

import React from "react";
import ReactDOM from "react-dom";

function render() {
  ReactDOM.render(
    <h1>Hello</h1>,
    document.getElementById("root")
  );
}
render();

Changing to this fixes it:

import * as React from "react";
import * as ReactDOM from "react-dom";

@DanielRosenwasser
Copy link
Member

This has to do with the CommonJS/ES module interop, which is not specified in the ECMAScript standard. In the near future, I believe that Babel and TypeScript will be more aligned on this.

@StokeMasterJack
Copy link
Author

The funny thing is that these are all runtime errors.

It's a bit ironic that after converting my code base to TypeScript, I'm now plagued with runtime errors.

Ezku added a commit to Ezku/laulu.jallu.rodeo that referenced this issue Aug 13, 2017
@Aqours
Copy link

Aqours commented Sep 21, 2017

Same issue.

@bluetech "allowSyntheticDefaultImports": true to your tsconfig.json if you want it to work like this.

This is temporary solution.
I think it's a bug when coding with JS and TS.

@rballonline
Copy link

I just had the same error, this was the first hit on a google search. Was strange to me as I was importing above just fine and then saw the difference:

import User from "user";

had the error. This:

import {User} from "user";

did not. Might help someone else.

@wk-j
Copy link

wk-j commented Feb 27, 2018

Try this "esModuleInterop": true

@ZhouYK
Copy link

ZhouYK commented Mar 8, 2018

@wk-j it works.thanks

@marknuzz
Copy link

@wk-j Your solution breaks my import of moment.js.

@wk-j
Copy link

wk-j commented Mar 13, 2018

@Mnuzz You can import moment like this

import moment from "moment"

@marknuzz
Copy link

Apparently not. I'll set up a sample project and show you what happens.

@shanvl
Copy link

shanvl commented Mar 23, 2018

Neither "allowSyntheticDefaultImports" nor "esModuleInterop" works in my case. Tried fresh CRA with react-scripts-ts and setting up from scratch with Typescript 2.7.2.

@felixrabe
Copy link

@Mnuzz did you get around to put together the sample project?

@gogoyqj
Copy link

gogoyqj commented Jul 9, 2018

need both

    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true

@haseose
Copy link

haseose commented Jul 27, 2018

@gogowings Thanks it's work.

@Jun711
Copy link

Jun711 commented Aug 3, 2018

I have set
"allowSyntheticDefaultImports": true
"esModuleInterop": true
but It didn't seem to work.
mine is Angular though

@wtilton
this is because user was a constant export

@hannah-hpcnt
Copy link

@Jun711 If your module is esnext, try opting in "moduleResolution": "node" in your tsconfig. (Reference)

@hamza00111
Copy link

Try this :
import {React} from "react";

@MartinYounghoonKim
Copy link

MartinYounghoonKim commented Oct 25, 2018

@gogoyqj Thanks. I solved this problem.

I hope my config helps other.

"compilerOptions": {
     "sourceMap": true,
    "module": "esnext",
    "moduleResolution": "node",
    "target": "es5",
    "jsx": "react",
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "typeRoots": [
      "./node_modules/@types/"
    ],
}

@gpresland
Copy link

Following the README, you are supposed to make the following changed in tslint.json:

{
-  "extends": ["tslint:recommended", "tslint-react", "tslint-config-prettier"],
+  "extends": [],
+  "defaultSeverity": "warning",
   "linterOptions": {
     "exclude": [
       "config/**/*.js",
       "node_modules/**/*.ts"
     ]
   }
 }

from https://github.com/Microsoft/TypeScript-React-Starter#overriding-defaults

You can also simply add:

+ "defaultSeverity": "warning",

And the issue should go simply be a warning in the console.

@JSEvgeny
Copy link

Combination of "allowSyntheticDefaultImports": true, "esModuleInterop": true and in addition "noImplicitAny": false did the trick for me, but I don't really like this solution

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests