62

I changed tsconfig.json by adding this properties

"esModuleInterop": true, "allowSyntheticDefaultImports": true,

in order to be able to import a npm package import * as ms from "ms";

But I still get this error

This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.

What am I missing?

Update:

If I change with import ms from "ms", then it works fine with the compiler but not with VSCode linter and the error is

 can only be default-imported using the 'allowSyntheticDefaultImports' flagts(1259)
index.d.ts(25, 1): This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.

As I said now is working but VSCode have a problem.

Georgian Stan
  • 1,865
  • 3
  • 13
  • 27

8 Answers8

79

"allowSyntheticDefaultImports": true

This flag solved problem, but it should be added to compilerOptions section of tsconfig.json

PokatilovArt
  • 1,111
  • 11
  • 13
53

You can just do something like this

import * as printJS from 'print-js'
Ever Dev
  • 1,882
  • 2
  • 14
  • 34
25

If you have this error trying to import localforage into angular 9, I used this (Angular 9.1.12, Typescript 3.9.6):

npm install localforage

// tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "esnext",
    "moduleResolution": "node",
    "importHelpers": true,
    "target": "es2015",
    "lib": [
      "es2018",
      "dom"
    ]
  },
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true,
    "esModuleInterop": true, <----------------------------add this
    "allowSyntheticDefaultImports": true <----------------------------and this
  },
}

// any component or service

import * as localforage from 'localforage';

constructor() {
    localforage.setItem('localforage', 'localforage value');
    setTimeout(() => {
      localforage.getItem('localforage').then((e) => {
       console.log(e);
      });
    }, 5000);
    setTimeout( async () => {
      const RES = await localforage.getItem('localforage');
      console.log('RES', RES);
    }, 10000);
  }
BlackICE
  • 8,816
  • 3
  • 53
  • 91
Mark
  • 370
  • 5
  • 10
  • 3
    worked after moving under compilerOptions in the //tsconfig.ts like below "compilerOptions": { "allowSyntheticDefaultImports":true, "esModuleInterop": true} – Aymen Boumaiza Sep 22 '21 at 12:50
  • 2
    be careful adding the `"esModuleInterop": true`, it can break things like `import * as moment from 'moment';` – BlackICE Oct 19 '21 at 14:07
4

The problem is how the package declared the export, you can still import using the default import:

import ms from "ms";
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
Lucas Sousa
  • 401
  • 5
  • 18
  • And how can I get over the linting error from VSCode, other then using @ts-ignore? – Georgian Stan Mar 30 '20 at 17:59
  • You can try using the node import: `let ms = require("./ms");` For more info [check this out](https://github.com/microsoft/TypeScript/issues/7185#issuecomment-354566288) – Lucas Sousa Mar 30 '20 at 18:26
4

it works perfectly importing all from the package module example :

import * as CryptoJS from 'crypto-js';

if this doesnt work make sure you are have successfully installed the package type

1

I solved this by adding "allowSyntheticDefaultImports": true to tsconfig.json

And you must save the changes by running this command line in the terminal:

tsc --p tsconfig.json
0

Having the error message

This module is declared with using 'export =', and can only be used with a default import when using the 'allowSyntheticDefaultImports' flag.

on a typescript import rule for an Angular component :

import { something } from 'amodule';

in node_modules/@types/amodule/index.d.ts, I found this code :

declare module 'amodule' {
   export = something;
}

and changed it to :

declare module 'amodule' {
    export default something;
}

the import rule in the angular component was changed to :

 import something from 'amodule';

and the error message is gone and the imported module can be used as expected.

  • 4
    Hacking someone else's modules is not recommended. Perhaps this is just a workaround to see something work, but not guaranteed to work in long term. What happens, for instance, the node_modules is git ignored and you do an `npm install` later? – sjsam Sep 22 '21 at 17:18
0

could be fixed by putting

export default

instead of

export =

Omid Farvid
  • 785
  • 7
  • 13