Angular 8 - Lazy loading modules : Error TS1323: Dynamic import is only supported when '--module' flag is 'commonjs' or 'esNext'

前端 未结 7 938
一个人的身影
一个人的身影 2020-12-04 15:09

When I updated Angular from 7 to Angular 8, getting error for lazy loading modules

I have tried the options, which are there in the angular upgradation guide

相关标签:
7条回答
  • 2020-12-04 15:21

    I resolved this issue only by adding "include": ["src/**/*.ts"] in my tsconfig.app.json file.

    0 讨论(0)
  • 2020-12-04 15:24

    You are using dynamic import so you have to change your tsconfig.json like this to target your code to esnext module

    {
      "compileOnSave": false,
      "compilerOptions": {
        "baseUrl": "./",
        "outDir": "./dist/out-tsc",
        "sourceMap": true,
        "declaration": false,
        "module": "esnext", // add this line
        "moduleResolution": "node",
        "emitDecoratorMetadata": true,
        "experimentalDecorators": true,
        "importHelpers": true,
        "target": "es2015",
        "typeRoots": [
          "node_modules/@types"
        ],
        "lib": [
          "es2018",
          "dom"
        ]
      }
    }
    

    Also make sure to check tsconfig.app.json dont have module and target config something like this

    {
      "extends": "./tsconfig.json",
      "compilerOptions": {
        "outDir": "./out-tsc/app",
        "types": []
      },
      "include": [
        "src/**/*.ts"
      ],
      "exclude": [
        "src/test.ts",
        "src/**/*.spec.ts"
      ]
    }
    
    0 讨论(0)
  • 2020-12-04 15:27

    Just adding to @Tony's anwser, you might also need to do the same (change to "module": "esnext" ) in the tsconfig.app.json. In my case the tsconfig.json was already using esnext as the module but the tsconfig.app.json was still using es2015 and that caused this error.

    0 讨论(0)
  • 2020-12-04 15:34

    I think the proper way to do this is to adjust tsconfig.app.json rather than tsconfig.json.

    tsconfig.app.json

    {
      "extends": "../tsconfig.json",
      "compilerOptions": {
        "outDir": "../out-tsc/app",
        "baseUrl": "./",
        "module": "esnext",
        "types": []
      },
      "exclude": [
        "test.ts",
        "**/*.spec.ts"
      ]
    }
    

    tsconfig.app.json is the Typescript configuration file specific to the app that sits beneath the root of the Angular workspace. The tsconfig.app.json exists so that if you are building an Angular workspace that has multiple apps in it, you can adjust the Typescript configuration separately for each app without having to write redundant configuration properties that overlap between the apps (hence the extends property).

    Technically, you don't need tsconfig.app.json at all. If you delete it, you will have to place the "module": "esnext" in tsconfig.json. If you keep it there, it will take precedence over tsconfig.json, so you only need to add the "module":"esnext" line in tsconfig.app.json.

    0 讨论(0)
  • 2020-12-04 15:38

    i resolve this error by by doing following steps step 1: "module": "es2015" to "module": "AMD" in tsconfig.json

    step 2: create a new file tsconfig.app.json in app root directory, copy code of Tony Ngo and paste into, then this problem will be resolved.

    0 讨论(0)
  • 2020-12-04 15:39

    Just want to add my experience to @Tony's answer. After changing tsconfig.json it still showed an error (red underline). Only after reopening the editor (I used VSCode) did I see the red underline disappear.

    0 讨论(0)
提交回复
热议问题