Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't do a default import in Angular 9

Tags:

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.

like image 503
Georgian Stan Avatar asked Mar 30 '20 17:03

Georgian Stan


People also ask

Can only be default imported using the esModuleInterop Flagts 1259?

The error "Module can only be default-imported using esModuleInterop flag" occurs when we try to import a CommonJS module into an ES6 module. To solve the error, set the esModuleInterop option to true in your tsconfig. json file.

What is allowSyntheticDefaultImports?

--allowSyntheticDefaultImports: Allow default imports from modules with no default export. This does not affect code emit, just typechecking.

Has no default export Did you mean to use?

The "Module has no default export" error occurs when we try to import as default from a module that doesn't have a default export. To solve the error make sure the module has a named export and wrap the import in curly braces, e.g. import {myFunction} from './myModule' .

Is the content of this article still applicable for angular 2+?

The content is likely still applicable for all Angular 2 + versions. In typical Angular applications, we can lazy load code via the Angular Modules and the Angular Router. The router is a great way to lazily load features as need and reduce our JavaScript payloads increasing application performance.

How to lazy load code in TypeScript Angular?

In typical Angular applications, we can lazy load code via the Angular Modules and the Angular Router. The router is a great way to lazily load features as need and reduce our JavaScript payloads increasing application performance. In the latest version of TypeScript (2.4) we get a new way to lazy load code: Dynamic Module Imports.

Is it possible to include unused modules in angular+Webpack?

But it seems that angular+webpack elides them out (tree shakes them) before bundling. While this does work, please note that some unused modules might be included. For example, if you use the regex / (de)\.js/, de.js and kde.js are included.


1 Answers

You can just do something like this

import * as printJS from 'print-js' 
like image 179
Ever Avatar answered Oct 19 '22 13:10

Ever