Reputation: 2129
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.
Upvotes: 68
Views: 116541
Reputation: 455
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
Upvotes: 2
Reputation: 367
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
Upvotes: 4
Reputation: 380
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);
}
Upvotes: 26
Reputation: 1390
"allowSyntheticDefaultImports": true
This flag solved problem, but it should be added to compilerOptions section of tsconfig.json
Upvotes: 92
Reputation: 95
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.
Upvotes: 1
Reputation: 2162
You can just do something like this
import * as printJS from 'print-js'
Upvotes: 58
Reputation: 411
The problem is how the package declared the export, you can still import using the default import:
import ms from "ms";
Upvotes: 4