Cannot use require in script
See original GitHub issueThe way the function constructor is created, it is not possible to use require directly.
Calling require in your script will result in: ReferenceError: require is not defined.
As a temporary hotfix, you can call the following before your require call:
const require = global.require || global.process.mainModule.constructor._load;
Solution was adapted from here.
It would be great if the function constructor was adapted to allow normal require calls.
Issue Analytics
- State:
- Created 4 years ago
- Comments:6 (2 by maintainers)
Top Results From Across the Web
require() not working in module type nodejs script
json file, I can't use require statements anymore, because I get a ReferenceError: require is not defined error. If I remove the "type":...
Read more >Javascript - How to fix ReferenceError: require is not defined
The JavaScript require() function is only available by default in Node.js environment. This means the browser won't know what you mean with the ......
Read more >Common Errors - RequireJS
9; Cannot use preserveLicenseComments and generateSourceMaps together§ 10 ... Be sure to load all scripts that call define() via the RequireJS API.
Read more >JavaScript require vs import - Flexiple
One of the major differences between require() and import() is that require() can be called from anywhere inside the program whereas import() ...
Read more >CommonJS modules | Node.js v19.3.0 Documentation
Calling require() always use the CommonJS module loader. ... The .mjs extension is reserved for ECMAScript Modules which cannot be loaded via require()...
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

I’ve just released version
0.3.0, andrequireis available when using that version! This does not solve the general dependency question @alexbde brings up, which I’ll think about separately.Thank you for opening this!
Hi! I missed a huge set of notifications being irresponsible with keyboard shortcuts 😆 I’m sorry! I’m looking into this and the open PR on this repo, as well.