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.

Dynamic imports don't work in nodejs

See original GitHub issue

🐛 Bug Report

Cannot dynamically import module in jest tests.

To Reproduce

Steps to reproduce the behavior:

test('dynami', async () => {
    const tenderElement = await import('mymodule');
});

Throws:

% npx jest test/dy.spec.js
(node:25196) ExperimentalWarning: The ESM module loader is experimental.
 FAIL  test/dy.spec.js
  ✕ dynami (1ms)

  ● dynami

    TypeError: A dynamic import callback was not specified.TypeError [ERR_VM_DYNAMIC_IMPORT_CALLBACK_MISSING]: A dynamic import callback wa
s not specified.

      1 | test('dynami', async () => {
    > 2 |     const tenderElement = await import('mymodule');
        |                           ^
      3 | });

      at Object.<anonymous> (test/dy.spec.js:2:27)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 total
Snapshots:   0 total
Time:        0.313s, estimated 1s
Ran all test suites matching /test\/dy.spec.js/i.

Expected behavior

Expected the dynamic import to work.

envinfo

% npx envinfo --preset jest
npx: installed 1 in 0.696s

  System:
    OS: Linux 5.3 Manjaro Linux
    CPU: (12) x64 Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
  Binaries:
    Node: 13.7.0 - /usr/bin/node
    Yarn: 1.22.0 - /usr/bin/yarn
    npm: 6.13.6 - /usr/bin/npm
  npmPackages:
    jest: ^24.9.0 => 24.9.0 

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

4reactions
SimenBcommented, Nov 6, 2020

dynamic import has been implemented in #10620, released in 26.5.3

0reactions
github-actions[bot]commented, May 11, 2021

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. Please note this issue tracker is not a help forum. We recommend using StackOverflow or our discord channel for questions.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Using the dynamic import() function on Node.js - Stack Overflow
The dynamic import() loads the module and return a module object that contains all its exports. In order to access the default export...
Read more >
Don't dynamically import code in a directory - ITNEXT
Some developers are following a trend of importing code by directory in NodeJS using the fs module. Example file: src/models/index.js. Benefits ...
Read more >
How to Dynamically Import ECMAScript Modules
Let's see how dynamic import works, and when it's useful. 1. Dynamic import of modules. When the import keyword is used as a...
Read more >
import - JavaScript - MDN Web Docs - Mozilla
There is also a function-like dynamic import() , which does not require scripts of type="module" . Syntax. import defaultExport from "module- ...
Read more >
How to dynamically import JavaScript modules - Flavio Copes
Find out how to import a JavaScript module dynamically. ... Maybe you're trying to load something from a folder but you don't know...
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