(Angular 12) The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script

Viewed 6447

I have the following dynamic import code to import a js library at runtime:

export class AuthService {
    constructor() {
        import('https://apis.google.com/js/platform.js').then(result => {
            console.log(result)
        })
    }
}

However when I try to build/serve the Angular project I get:

external "https://apis.google.com/js/platform.js" - Error: The target environment doesn't support dynamic import() syntax so it's not possible to use external type 'module' within a script Did you mean to build a EcmaScript Module ('output.module: true')?

In addition, vs-code highlights the import with the following:
enter image description here

I tried adding "type": "module" in package.json and "module": "ESNext" in tsconfig.json, but with no luck.

How can I fix this?

1 Answers

Dynamic imports only works since ES2020. Please, check your tsconfig.json in order to know what version is your target.

Related