question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when using dynamic import -- support `esnext`

See original GitHub issue

What happens and why it is wrong

When using dynamic imports the following error is thrown:

[!] (rpt2 plugin) Error: /home/leo/development/other/rollup-plugin-typescript2-debug/a.ts(1,1): semantic error TS1323 Dynamic import cannot be used when targeting ECMAScript 2015 modules.
a.ts
Error: /home/leo/development/other/rollup-plugin-typescript2-debug/a.ts(1,1): semantic error TS1323 Dynamic import cannot be used when targeting ECMAScript 2015 modules.
    at error (/home/leo/development/other/rollup-plugin-typescript2-debug/node_modules/rollup/dist/rollup.js:170:15)
    at /home/leo/development/other/rollup-plugin-typescript2-debug/node_modules/rollup/dist/rollup.js:17346:17
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
    at Function.Module.runMain (module.js:667:11)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:607:3

This is caused by this module forcefully setting the module compiler option to es2015.

Environment

a.ts

import('./b').then(({ message }) => console.log(message));

b.ts

export const message = 'it works!';

Versions

  • typescript: 2.6.2
  • rollup: 0.53.4
  • rollup-plugin-typescript2: 0.9.0

rollup.config.js

"use strict";

import typescript from "rollup-plugin-typescript2";

export default {
  input: "./a.ts",
  output: {
    format: "cjs",
  },

  plugins: [
    typescript({
      tsconfig: './tsconfig.json',
      verbosity: 3,
    })
  ]
}

tsconfig.json

{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2015"
  }
}

plugin output with verbosity 3

rpt2: typescript version: 2.8.0-dev.20180131
rpt2: rollup-plugin-typescript2 version: 0.9.0
rpt2: plugin options:
{
    "tsconfig": "./tsconfig.json",
    "verbosity": 3,
    "check": true,
    "clean": false,
    "cacheRoot": "/home/leo/development/other/rollup-plugin-typescript2-debug/.rpt2_cache",
    "include": [
        "*.ts+(|x)",
        "**/*.ts+(|x)"
    ],
    "exclude": [
        "*.d.ts",
        "**/*.d.ts"
    ],
    "abortOnError": true,
    "rollupCommonJSResolveHack": false,
    "useTsconfigDeclarationDir": false,
    "typescript": "version 2.8.0-dev.20180131",
    "tsconfigOverride": {}
}
rpt2: rollup config:
{
    "input": "./a.ts",
    "plugins": [
        {
            "name": "rpt2"
        }
    ],
    "entry": "./a.ts",
    "external": [
        "",
        ""
    ]
}
rpt2: built-in options overrides: {
    "module": 5,
    "noEmitHelpers": true,
    "importHelpers": true,
    "noResolve": false,
    "noEmit": false,
    "outDir": "/home/leo/development/other/rollup-plugin-typescript2-debug",
    "moduleResolution": 2
}
rpt2: parsed tsconfig: {
    "options": {
        "module": 5,
        "target": 2,
        "outDir": "/home/leo/development/other/rollup-plugin-typescript2-debug",
        "baseUrl": "/home/leo/development/other/rollup-plugin-typescript2-debug/src/",
        "sourceMap": true,
        "strict": true,
        "experimentalDecorators": true,
        "noImplicitReturns": true,
        "allowSyntheticDefaultImports": true,
        "allowUnreachableCode": false,
        "noEmitHelpers": true,
        "importHelpers": true,
        "noResolve": false,
        "noEmit": false,
        "moduleResolution": 2,
        "configFilePath": "/home/leo/development/other/rollup-plugin-typescript2-debug/./tsconfig.json"
    },
    "fileNames": [
        "/home/leo/development/other/rollup-plugin-typescript2-debug/a.ts",
        "/home/leo/development/other/rollup-plugin-typescript2-debug/b.ts",
        "/home/leo/development/other/rollup-plugin-typescript2-debug/src/a.ts",
        "/home/leo/development/other/rollup-plugin-typescript2-debug/src/main.ts",
        "/home/leo/development/other/rollup-plugin-typescript2-debug/src/types.d.ts",
        "/home/leo/development/other/rollup-plugin-typescript2-debug/src/utils/ErrorMapper.ts"
    ],
    "typeAcquisition": {
        "enable": false,
        "include": [],
        "exclude": []
    },
    "raw": {
        "compilerOptions": {
            "module": "commonjs",
            "target": "es2015",
            "outDir": "dist",
            "baseUrl": "src/",
            "sourceMap": true,
            "strict": true,
            "experimentalDecorators": true,
            "noImplicitReturns": true,
            "allowSyntheticDefaultImports": true,
            "allowUnreachableCode": false
        },
        "exclude": [
            "node_modules"
        ]
    },
    "errors": [],
    "wildcardDirectories": {
        "/home/leo/development/other/rollup-plugin-typescript2-debug": 1
    },
    "compileOnSave": false,
    "configFileSpecs": {
        "includeSpecs": [
            "**/*"
        ],
        "excludeSpecs": [
            "node_modules"
        ],
        "validatedIncludeSpecs": [
            "**/*"
        ],
        "validatedExcludeSpecs": [
            "node_modules"
        ],
        "wildcardDirectories": {
            "/home/leo/development/other/rollup-plugin-typescript2-debug": 1
        }
    }
}
rpt2: included:
'[
    "*.ts+(|x)",
    "**/*.ts+(|x)"
]'
rpt2: excluded:
'[
    "*.d.ts",
    "**/*.d.ts"
]'
rpt2: Ambient types:
rpt2:     /home/leo/development/other/rollup-plugin-typescript2-debug/src/types.d.ts
rpt2:     /home/leo/development/other/rollup-plugin-typescript2-debug/node_modules/@types/lodash/index.d.ts
rpt2:     /home/leo/development/other/rollup-plugin-typescript2-debug/node_modules/@types/node/index.d.ts
rpt2:     /home/leo/development/other/rollup-plugin-typescript2-debug/node_modules/@types/screeps/index.d.ts
rpt2: ambient types changed, redoing all semantic diagnostics
rpt2: transpiling '/home/leo/development/other/rollup-plugin-typescript2-debug/a.ts'
rpt2:     cache: '/home/leo/development/other/rollup-plugin-typescript2-debug/.rpt2_cache/cd81cf42f3dcf638d9c3e61868cbf69ad7637cd0/code/cache/f11ac0bffbde51209d9a516866cafbe3aca2435c'
rpt2:     cache miss
rpt2:     cache: '/home/leo/development/other/rollup-plugin-typescript2-debug/.rpt2_cache/cd81cf42f3dcf638d9c3e61868cbf69ad7637cd0/syntacticDiagnostics/cache/f11ac0bffbde51209d9a516866cafbe3aca2435c'
rpt2:     cache miss
rpt2:     cache: '/home/leo/development/other/rollup-plugin-typescript2-debug/.rpt2_cache/cd81cf42f3dcf638d9c3e61868cbf69ad7637cd0/semanticDiagnostics/cache/f11ac0bffbde51209d9a516866cafbe3aca2435c'
rpt2:     cache miss
[!] (rpt2 plugin) Error: /home/leo/development/other/rollup-plugin-typescript2-debug/a.ts(1,1): semantic error TS1323 Dynamic import cannot be used when targeting ECMAScript 2015 modules.
a.ts
Error: /home/leo/development/other/rollup-plugin-typescript2-debug/a.ts(1,1): semantic error TS1323 Dynamic import cannot be used when targeting ECMAScript 2015 modules.
    at error (/home/leo/development/other/rollup-plugin-typescript2-debug/node_modules/rollup/dist/rollup.js:170:15)
    at /home/leo/development/other/rollup-plugin-typescript2-debug/node_modules/rollup/dist/rollup.js:17346:17
    at <anonymous>
    at process._tickCallback (internal/process/next_tick.js:188:7)
    at Function.Module.runMain (module.js:667:11)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:607:3

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ezolenkocommented, Mar 2, 2018

@shunia before esnext became available, es2015 was the only option you could use for rollup to understand the output. In such cases where there can be only one option that could possibly work, I override the value (other examples are moduleResolution, noEmit, etc).

Now that there is more than one option, I’ll have to relax the override (will probably take the value from tsconfig and warn if values is incompatible with rollup).

0reactions
ezolenkocommented, May 25, 2022

In worst case (module: "commonjs") rollup will simply not see any imports and will only ever transpile and bundle the entry file keeping requires as they are. (Well, I didn’t check other cases, but that one as bad as it gets: no errors and broken output).

0.12.0 is now on npm, that one will take tsconfig value, accepting es2015 and esnext, and bail out on anything else.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Lazy loading modules : Error TS1323: Dynamic import is only ...
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...
Read more >
Error when trying to use dynamic import with module: "es2015"
I get error Dynamic import cannot be used when targeting ECMAScript 2015 modules. ... es modules but it doesn't make sense because import()...
Read more >
Dynamic imports are only supported when the 'module' flag is ...
To solve the error "Dynamic imports are only supported when the '--module' flag is set to 'es2020', 'esnext' or 'commonjs'", set the module...
Read more >
ES-Next dynamic import - DEV Community ‍ ‍
import item from './itemA'; it automatically loads the default export of module 'itemA' into the item . But when we do expression like...
Read more >
Documentation - TypeScript 2.4
Dynamic import expressions are a new feature and part of ECMAScript that allows users to asynchronously request a module at any arbitrary point...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found