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 import only work when direct passed named

See original GitHub issue

Hi i am working on a offline approach to use with module federation and to achieve this i must load and cache all the components from my remotes. But when i try to load the component with dynamic import and dynamic string, the webpack cant find it module. I am targeting to do

const modules = ["Cronograma", "Sampler", "Wrapper"]
modules.forEach(module => import(`cronograma/${module}`)) 

I saw it didnt work and try to simplify it in order to test. I’ve tried a simples dummy test like:

    const moduleName = "cronograma/Cronograma";
    import(moduleName);

At the browser i am getting the error:

src_bootstrap_js.js:2169 Uncaught (in promise) Error: Cannot find module ‘cronograma/Cronograma’

According with the Dynamic import specs it accepts a string, so it appears to be something related with module federation 😕

Here the full code of my test

// const modules = ["Cronograma", "Sampler", "Wrapper"]
const modules = ["Cronograma"]
setTimeout(() => {
    //modules.forEach(module => import(`cronograma/${module}`)) Does not work either
    console.log("Trying to import from string ")
    const moduleName = "cronograma/Cronograma";
    import(moduleName); //Dont work
    setTimeout(() => {
        console.log("Trying to import the same but directly string ")
        import("cronograma/Cronograma"); // Work
        console.log("Imported")
    }, 1000)
}, 3000)

Above the screencapture of the browser (and the componented rendered using the plain import withtout the string)

image

I am letting something pass by? or should work in a different way with string dynamic imports with module federation?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:28 (24 by maintainers)

github_iconTop GitHub Comments

2reactions
ScriptedAlchemycommented, Jul 29, 2022

So I may have a way to do this now. It would involve inspecting the runtime module that creates this request, and see if it can take a dynamic expression for it.

2reactions
schirrelcommented, Apr 11, 2022
Read more comments on GitHub >

github_iconTop Results From Across the Web

ES2020: `import()` – dynamically importing ES modules - 2ality
First, this import declaration can only appear at the top level of a module. That prevents you from importing modules inside an if...
Read more >
How can I import a module dynamically given its name as ...
The function imports the module name , potentially using the given globals and locals to determine how to interpret the name in a...
Read more >
Dynamic imports solve all the problems, right? - Minko Gechev
Modern bundlers and TypeScript can resolve dynamic imports only when we have specified the module with a string literal (an exception is webpack ......
Read more >
Dynamically Importing Components with React.lazy
In its function form, import returns a Promise that resolves with the value of the exported module once the network request has fetched...
Read more >
Dynamic imports - The Modern JavaScript Tutorial
The module path must be a primitive string, can't be a function call. ... That's possible only because the structure of imports/exports is ......
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