You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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?
Reacted by Alex0007, stefanpl, Eyal Kalderon and Jerome De Leon
The import x = require('x') syntax is discouraged in use for modern code bases, since it's non-standard. Turning on esModuleInteropis 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.
@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?
Reacted by itsMapleLeaf, Nick Kang, Eyal Kalderon and Sandro Böhme
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.
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?
Activity
stefanpl commentedon Apr 16, 2020
@Baterka you can import (without the
esModuleInteropflag) by using this syntax:itsMapleLeaf commentedon Jun 5, 2020
The
import x = require('x')syntax is discouraged in use for modern code bases, since it's non-standard. Turning onesModuleInteropis 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 commentedon Jun 7, 2020
@kingdaro thanks for your comment! I had not been aware that the
import x = requiresyntax is non-standard, I'll try to avoid it in the future. Problem is, last time I triedesModuleInterop, it was not working very smoothly. Guess I'll have to do some digging there as well.Is migrating the
mspackage to the ES module format an option? I guess that would be the gist of this ticket, wouldn't it?Baterka commentedon Dec 16, 2020
Why closed?
vtrphan commentedon Dec 17, 2020
i'm having problem too. why closed?
JeromeDeLeon commentedon Dec 26, 2020
Same here, I inserted the
allowSyntheticDefaultImportsandesModuleInteropbut still doingimport ms from 'ms'still gives meundefinedwhen 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 commentedon Apr 22, 2021
same for me!
I think this issue should be reopened
guiseek commentedon 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/msproject? there should be a notice there stating that it's no longer needed, no?guiseek commentedon Oct 17, 2021
the solution might be to install the canary version, like this:
npm i ms@3.0.0-canary.1and import as follows:
import ms, { StringValue } from 'ms'are now using string types, the code would look like this: