Skip to content
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

Closed
subvertallchris opened this issue Dec 7, 2016 · 17 comments
Closed

Comments

@subvertallchris
Copy link

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!

@rawrmaan
Copy link

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
Copy link
Author

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
Copy link

vittau commented Feb 6, 2017

Did you manage to solve this problem?

@subvertallchris
Copy link
Author

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

@vittau
Copy link

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
Copy link

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
Copy link

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
Copy link

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

@cameronsjo
Copy link

cameronsjo 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
Copy link

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
Copy link

Any news on this?

@william-holt
Copy link

@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
Copy link

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
Copy link

sleagon commented Aug 24, 2018

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

@olavoparno
Copy link

Guys, here's what worked for me. (Working with TS 2.7+).

microsoft/TypeScript#13375 (comment)

@marwahaha
Copy link
Member

Is this still an issue with TS and Babel?
It would be great to update the docs if it may help:
https://momentjs.com/docs/#/use-it/typescript/

@marwahaha
Copy link
Member

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

No branches or pull requests