Error: Cannot find module with dynamic import

See original GitHub issue

Do you want to request a feature or report a bug?

bug

What is the current behavior?

When I try to load dynamic module with import('/path/to/my/module.js').then() it work, but when i try to import module with import(rootPath + '/' + myModuleName + '/index.js').then() it doesn’t work.

I got:

 Error: Cannot find module '/Users/hubert_i/Emodyz/launcher-ezgames.eu/modules/test/index.js'.
      at /Users/hubert_i/Emodyz/launcher-ezgames.eu/dist/electron/main.js:10475:9
      at <anonymous>
      at process._tickCallback (internal/process/next_tick.js:188:7)

If the current behavior is a bug, please provide the steps to reproduce.

What is the expected behavior?

Dynamic import with variable

Please mention other relevant information such as the browser version, Node.js version, webpack version, and Operating System.

webpack => 3.10.0 npm => 5.6.0 yarn => 1.5.1 Node.js => v9.6.1 Operating System => macOs High Sierra

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:29
  • Comments:30 (3 by maintainers)

github_iconTop GitHub Comments

234reactions
ooflorentcommented, Oct 15, 2018

Webpack performs a static analyse at build time. It doesn’t try to infer variables which that import(test) could be anything, hence the failure. It is also the case for import(path+"a.js").

If you need truly dynamic imports, you have to restrict it to a known path:

import("./locales/" + locale + ".js")

In the above example, webpack will create a chunk for each JS file in locale/.

103reactions
leohubertcommented, Mar 6, 2018

It’s incomprehensible 😢

If i write this it work

import( '/Users/hubert_i/Emodyz/launcher-ezgames.eu/modules/test/index.js')
  .then((m) => {
    console.log(m.name)
  })
  .catch(err => {
    console.error('Error during loading module: ' + err)
    callback()
  })

but when i write this don’t work

const test = '/Users/hubert_i/Emodyz/launcher-ezgames.eu/modules/test/index.js'
import(test)
  .then((m) => {
    console.log(m.name)
  })
  .catch(err => {
    console.error('Error during loading module: ' + err)
    callback()
  })
Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught Error: Cannot Find Module When Using Dynamic ...
Fully dynamic statements, such as import(foo), will fail because webpack requires at least some file location information.The import() must ...
Read more >
Error: Cannot find module with dynamic import - Allen Kim
If you want component name as a variable​​ const imported: any = await import(`./${name}. component`); const komponent = imported[`MyComponent`]; const ...
Read more >
Module Methods - webpack
A normal import statement cannot be used dynamically within other logic or contain variables. See the spec for more information and import() below...
Read more >
@rollup/plugin-dynamic-import-vars - npm
Resolving dynamic imports that contain variables.. Latest version: 2.0.2, last published: 10 days ago.
Read more >
Uncaught Error: Cannot Find Module When Using Dynamic ...
[Solved]-Uncaught Error: Cannot Find Module When Using Dynamic Import for JavaScript-Reactjs ... Only partially dynamic statement are allowed for import().
Read more >

github_iconTop Related Medium Post

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