Skip to content

Error when trying to use dynamic import with module: "es2015" #16675

@Nitive

Description

@Nitive

TypeScript Version: 2.4.0

Code

import('./utils')
  .then(({ sum }) => {
    console.log(sum(1, 2))
  })
  .catch(console.error)

tsconfig.json (short version)

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "es2015"
  }
}

Expected behavior:
No errors

Actual behavior:
Dynamic import cannot be used when targeting ECMAScript 2015 modules. with non-obvious reasons why.

Example repository


I get error Dynamic import cannot be used when targeting ECMAScript 2015 modules.
when I try to use dynamic import inside typescript project with "module": "es2015"
in configuration. The error message makes it clear that I can't use import() with
es modules but it doesn't make sense because import() is part of es modules.

However, it compiles correctly but throw annoing error.

I created a repository to reproduce an error.
npm run build-ts: compile ts files into build/ folder.
npm run build-webpack: compile js into dist/. (Then open index.html to see that everything works fine)

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScriptDomain: Error MessagesThe issue relates to error messagingFixedA PR has been merged for this issueGood First IssueWell scoped, documented and has the green light

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions