Skip to content

TypeScript import without 'esModuleInterop' #127

Closed

Description

@Baterka

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?

Activity

  1. stefanpl commented on Apr 16, 2020

    @stefanpl

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

    import ms = require('ms');
  2. itsMapleLeaf commented on Jun 5, 2020

    @itsMapleLeaf

    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.

  3. stefanpl commented on Jun 7, 2020

    @stefanpl

    @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?

  4. Baterka commented on Dec 16, 2020

    @Baterka
    Author

    Why closed?

  5. vtrphan commented on Dec 17, 2020

    @vtrphan

    i'm having problem too. why closed?

  6. JeromeDeLeon commented on Dec 26, 2020

    @JeromeDeLeon

    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
      }
    }
    `
  7. homj commented on Apr 22, 2021

    @homj

    same for me!
    I think this issue should be reopened

  8. guiseek commented on Oct 17, 2021

    @guiseek

    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?

  9. guiseek commented on Oct 17, 2021

    @guiseek

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions