Cannot use declared variables in import inside dynamic callback
See original GitHub issueBug report
Describe the bug
I’m currently using v9.5.0
and when attempting to use a variable defined inside the callback of dynamic
it throws an error: ReferenceError: locale is not defined
.
Although if I move the variable definition to outside the function it works fine.
To Reproduce
Code snippet:
Failed example:
const DynamicComponent = dynamic(
() => {
const locale = getUserLocale();
console.log(locale); // It doesn't even reach here ...
return import(`~/translations/${locale}.json`)
.then(handleFileContent)
},
{ loading: () => <p>Loading...</p>, ssr: false }
)
// error output: "ReferenceError: locale is not defined"
Working example:
const locale = getUserLocale();
const DynamicComponent = dynamic(
() => {
console.log(locale); // console.log works as expected
return import(`~/translations/${locale}.json`)
.then(handleFileContent)
},
{ loading: () => <p>Loading...</p>, ssr: false }
)
Expected behavior
Variables declared inside the callback of dynamic
should be allowed to use during import
call.
System information
- OS: macOs
- Version of Next.js: 9.5.0
- Version of Node.js: 13.14.0
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:12 (1 by maintainers)
Top Results From Across the Web
Dynamic ES6 module scopes - javascript - Stack Overflow
So by saying dynamic module scope I mean that module variables can be defined/loaded runtime, which will allow to extend ES6 syntax to...
Read more >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 >Understand Dynamic Importing in JavaScript
To dynamically import a module, we need to use import keyword as a function. When used as a function, this will return a...
Read more >Advanced Features: Dynamic Import - Next.js
dynamic () can't be used inside of React rendering as it needs to be marked in the top level of the module for...
Read more >Dynamic imports - The Modern JavaScript Tutorial
First, we can't dynamically generate any parameters of import . ... Or, we could use let module = await import(modulePath) if inside an ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
So, this is my setup, I have forked the
with-dynamic-export
example locally to replicate the above scenario:When I go and look at the
index.js
inside.next
folder, these two components are resolved differently. That’s why I think something is wrong with Webpack when using variables insideimport
(have added new lines for readability)In the first case, it is using something called
__webpack_require__.e(/*! import() */ 0)
In the second case, it basically generated its own helper function and using that
__webpack_require__(\"./components lazy recursive ^\\\\.\\\\/.*$\")
and calling it with./${componentName}
I am not much of an expert with Webpack so I don’t know if this is helpful or not, but this is what I was able to find.
@MihirGH while you’re at it can you output the following example:
Because this example is working for me and maybe we can compare with your previous one and see if we can see any major difference.