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.

Module API require with dynamic load module causes variable not defined

See original GitHub issue

bug? 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 2017-07-12 5 41 59

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:closed
  • Created 6 years ago
  • Reactions:1
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

3reactions
dvshurcommented, Jul 17, 2017

You can stick with CommonJS imports as a workaround until it’s fixed. It’s not even that dirty 🙂.

// import data from './data'
const data = require('./data').default;

console.log('require dynamic', require(`./${data.dynamic}.js`).default); // this works

It works because a CommonJS module is treated like a regular object and its fields don’t get renamed by webpack.

0reactions
alexander-akaitcommented, Nov 9, 2020

Closing due to inactivity. Please test with latest version and feel free to reopen if still regressions. Thanks!

Read more comments on GitHub >

github_iconTop 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 >

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