Can't do a default import in Angular 9
Solution 1
The problem is how the package declared the export, you can still import using the default import:
import ms from "ms";
Solution 2
You can just do something like this
import * as printJS from 'print-js'
Solution 3
"allowSyntheticDefaultImports": true
This flag solved problem, but it should be added to compilerOptions section of tsconfig.json
Solution 4
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);
}
![Georgian Stan](https://lh3.googleusercontent.com/-cEWTknUn130/AAAAAAAAAAI/AAAAAAAAAAA/ACHi3rfX6Q2F4nriojJauxtZcqZ38yxYcQ/mo/photo.jpg?sz=256)
Georgian Stan
Updated on July 09, 2022Comments
-
Georgian Stan almost 2 years
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 iscan 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.