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

TypeScript import without 'esModuleInterop' #127

Closed
Baterka opened this issue Feb 20, 2020 · 9 comments
Closed

TypeScript import without 'esModuleInterop' #127

Baterka opened this issue Feb 20, 2020 · 9 comments

Comments

@Baterka
Copy link

Baterka commented Feb 20, 2020

When importing into TypeScript file with @types/ms installed I am getting:

TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export.

Sure, I can turn esModuleInterop, but no other module I used had this issue.

Fix can be maybe to update code?

@stefanpl
Copy link

@Baterka you can import (without the esModuleInterop flag) by using this syntax:

import ms = require('ms');

@itsMapleLeaf
Copy link

The import x = require('x') syntax is discouraged in use for modern code bases, since it's non-standard. Turning on esModuleInterop is the real fix here.

The module is written in CJS, so if you're using ES modules, interop is needed to import it, as a default.

@stefanpl
Copy link

stefanpl commented Jun 7, 2020

@kingdaro thanks for your comment! I had not been aware that the import x = require syntax is non-standard, I'll try to avoid it in the future. Problem is, last time I tried esModuleInterop, it was not working very smoothly. Guess I'll have to do some digging there as well.

Is migrating the ms package to the ES module format an option? I guess that would be the gist of this ticket, wouldn't it?

@styfle styfle closed this as completed Dec 7, 2020
@Baterka
Copy link
Author

Baterka commented Dec 16, 2020

Why closed?

@vtrphan
Copy link

vtrphan commented Dec 17, 2020

i'm having problem too. why closed?

@JeromeDeLeon
Copy link

Same here, I inserted the allowSyntheticDefaultImports and esModuleInterop but still doing import ms from 'ms' still gives me undefined when accessing it. I scaffolded the project based on the NestJS setup.

{
  "compilerOptions": {
    "module": "commonjs",
    "declaration": true,
    "removeComments": true,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "allowSyntheticDefaultImports": true,
    "esModuleInterop": true,
    "target": "es2017",
    "sourceMap": true,
    "outDir": "./dist",
    "baseUrl": "./",
    "incremental": true
  }
}
`

@homj
Copy link

homj commented Apr 22, 2021

same for me!
I think this issue should be reopened

@guiseek
Copy link

guiseek commented Oct 17, 2021

https://www.npmjs.com/package/@types/ms

is this library already typescript in your source? why are you still maintaining the @types/ms project? there should be a notice there stating that it's no longer needed, no?

@guiseek
Copy link

guiseek commented Oct 17, 2021

the solution might be to install the canary version, like this:
npm i ms@3.0.0-canary.1

and import as follows:
import ms, { StringValue } from 'ms'

are now using string types, the code would look like this:

export class DateInterval {
   public readonly ms: number;

   constructor(interval: StringValue) {
     this.ms = ms(interval);
   }
}

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

No branches or pull requests

8 participants