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.

Cannot use require in script

See original GitHub issue

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

github_iconTop GitHub Comments

2reactions
jclemcommented, Nov 13, 2019

I’ve just released version 0.3.0, and require is 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!

0reactions
jclemcommented, Nov 13, 2019

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.

Read more comments on GitHub >

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

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