Module API require with dynamic load module causes variable not defined
See original GitHub issuebug? Is the module context error? In the code, why throw error ‘Uncaught ReferenceError’.
index.js
import data from './data';
import dynamic from './dynamic';
const _dynamic = {
dynamic: 'dynamic',
};
console.log('data', data);
console.log('dynamic', dynamic);
console.log('require', require('./dynamic.js').default);
console.log('require _dynamic', require(`./${_dynamic.dynamic}.js`).default);
console.log('require dynamic', require(`./${data.dynamic}.js`).default); // throw Error Uncaught ReferenceError: data is not defined
data.js
export default {
dynamic: 'dynamic',
};
dynamic.js
export default {
key: 'value',
};
Chrome console
If use the babel module, it will not report the error.
Chrome 59 Node.js 6.10.1 webpack 2.6.1
Issue Analytics
- State:
- Created 6 years ago
- Reactions:1
- Comments:5 (4 by maintainers)
Top Results From Across the Web
ReferenceError: require is not defined in ES module
js to a dynamic import() which is available in all CommonJS modules. which I think it's related to the fact that The current...
Read more >Modules · The Julia Language
When a global variable is encountered that has no definition in the current module, the system will search for it among variables exported...
Read more >Errors | Node.js v19.3.0 Documentation
The error.cause property is typically set by calling new Error(message, { cause }) . It is not set by the constructor if the...
Read more >Output - webpack
output.asyncChunks. boolean = true. Create async chunks that are loaded on demand. webpack.config.js module ...
Read more >API - ESBuild
In node, you can load a module using node --experimental-modules file.mjs . Note that node requires the .mjs extension unless you have configured...
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 Free
Top 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
You can stick with CommonJS imports as a workaround until it’s fixed. It’s not even that dirty 🙂.
It works because a CommonJS module is treated like a regular object and its fields don’t get renamed by webpack.
Closing due to inactivity. Please test with latest version and feel free to reopen if still regressions. Thanks!