New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import with Typescript and Babel VS import with just Typescript #3650

Open
subvertallchris opened this Issue Dec 7, 2016 · 14 comments

Comments

Projects
None yet
@subvertallchris

subvertallchris commented Dec 7, 2016

I'm using Moment 2.17.1 with TypeScript 2.1.1, OS X (10.11.6) and Node 6.3.0. I'm having trouble importing moment consistently.

In our dev and prod environments, we use Babel to compile. In that scenario, this import statement works:

import moment from 'moment';

This works in the browser but causes TypeError: moment_1.default is not a function in my mocha tests.

Our test code is built without babel, just tsc to an output directory and we run from there. In that case, we have to use this statement:

import * as moment from 'moment';

This makes node happy but brings back the dreaded moment is not a function(…) error when that code is hit in the browser.

Naturally, I need to find one import to rule them all if I want to test any code that uses moment.

I have enabled allowSyntheticDefaultImports in both environments' tsconfigs. My guess is that the presence of Babel is complicating things but I don't know what the solution is. Would love some advice. Thank you!

@icambron icambron added the TypeScript label Dec 12, 2016

@rawrmaan

This comment has been minimized.

rawrmaan commented Dec 14, 2016

I would recommend against using allowSyntheticDefaultImports and sticking with the import * as moment from 'moment' syntax with tsc. That compiler option has never worked the way I expected and always caused issues like this.

@subvertallchris

This comment has been minimized.

subvertallchris commented Dec 15, 2016

As I tried to describe in the issue, doing that results in moment is not a function death, I guess because of something have to do with Babel. I'd love suggestions that would lead to the resolution of that.

@vittau

This comment has been minimized.

vittau commented Feb 6, 2017

Did you manage to solve this problem?

@subvertallchris

This comment has been minimized.

subvertallchris commented Feb 6, 2017

No. I changed my testing approach to work around it. Still curious about a solution.

@vittau

This comment has been minimized.

vittau commented Feb 6, 2017

I see. I'm having a similar problem trying to AoT-compile Angular2 (which requires me to use import moment from 'moment') and still work with JIT (import * as moment from 'moment'). I simply cannot find an approach that will work with both.

@hojberg

This comment has been minimized.

hojberg commented Apr 17, 2017

Anyone figured out a solution to this? Moment doesn't seem to be usable with TypeScript at all due to this...

@hojberg

This comment has been minimized.

hojberg commented Apr 17, 2017

A way to get this to work is to output CommonJS in the module setting. Babel will be happy with require

@opavader

This comment has been minimized.

opavader commented Apr 27, 2017

@hojberg Can you please elaborate your suggested fix (or tell what needs to be changed where for the noobs)

@cameron-sjo

This comment has been minimized.

cameron-sjo commented May 9, 2017

@opavader I've been struggling with this for weeks now. It finally just dawned on me.

In your tsconfig.json add the following line:

`"module": "commonjs"`

So my complete file looks like this:

{
  "compilerOptions": {
    "baseUrl": ".",
    "moduleResolution": "node",
    "target": "es6",
    "module": "commonjs",
    "jsx": "preserve",
    "sourceMap": true,
    "skipDefaultLibCheck": true,
    "paths": {
      // Fix "Duplicate identifier" errors caused by multiple dependencies fetching their own copies of type definitions.
      // We tell TypeScript which type definitions module to treat as the canonical one (instead of combining all of them).
      "history": ["./node_modules/@types/history/index"],
      "react": ["./node_modules/@types/react/index"]
    }
  },
  "exclude": [
      "bin",
      "node_modules"
  ]
}

YMMV but this seems to work with my configuration with Webpack, Typescript, and all the other stuff my template created for me.

Edit; This broke some functionality such as isometric-fetch in my site.

@opavader

This comment has been minimized.

opavader commented May 10, 2017

I am already using commonjs for building but still getting the error.
(also, you can use https://github.com/developit/unfetch for fetch. Its much more smaller & easy to use.)

@darkbasic

This comment has been minimized.

darkbasic commented Oct 12, 2017

Any news on this?

@william-holt

This comment has been minimized.

william-holt commented Nov 1, 2017

@darkbasic I was running into this issue as well, but just discovered a fix for my own situation. I deleted the entire node_modules directory and the package-lock.json file, then reinstalled all of the modules, rebuilt the project and the error went away. The Sr. dev on my team thinks that one of the packages was slightly out of sync and causing the issue, this may not be the case for everyone but it might help.

ikr added a commit to ikr/react-period-of-stay-input that referenced this issue Aug 8, 2018

@sleagon

This comment has been minimized.

sleagon commented Aug 24, 2018

@subvertallchris @rawrmaan @vittau hello guys, is there any one fixed this issue? i just run into this issue, you can reproduce it with project here. Of cause, you can fix it by modify es6 module to commonjs module in tsconfig.json or just rewrite import * as moment from 'moment' toconst moment = require('moment'), however that is not what i want, i just want it works nicely without changing my code style. Thank you very much.

@sleagon

This comment has been minimized.

sleagon commented Aug 24, 2018

by the way, issue has been posted in webpack and Stack Overflow

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment