-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReferenceError: require is not defined #33741
Comments
cc @nodejs/modules-active-members |
Hey thank you for the report and engaging, this is actually intentional. You cannot import { createRequire } from 'module';
const require = createRequire(import.meta.url);
// can now use `require` in an ESM See this section in the docs regarding why and how to work around it. (I'm closing since I don't think this is a bug - as usual, if anyone feels strongly feel free to reopen) |
Can you verify that this happens in any directory? By default a .js file shouldn’t be treated as a module. I suspect that you may be in a directory (or nested directory below) where a package.json has a “type” field set to “module”. |
BTW: Apologies, I somehow missed that OP did not realize they were running inside module context 🙏 Thanks for the comment Jan. |
Let's reopen for now. |
@jkrems You are right. The module "type" was causing the issue. Thank you for flagging that out. |
We could perhaps expand the error message to include something like “file.js is loaded as an ES module due to /path/to/package.json containing "type": "module"” |
Yes. That will be very helpful! |
Let's reopen it the meantime for the error message improvement. |
I could work on this |
Thanks for this thread, was facing the same issue. More precise error message would definitely help people. |
Thanks so much, I was having this issue using a type:module in package.json but now it's working thanks to your solution. |
ReferenceError: createRequire is not defined |
@priya-concetto did you import it from "module" ? |
Fixes: #33741 PR-URL: #37852 Reviewed-By: Guy Bedford <[email protected]>
Fixes: #33741 PR-URL: #37852 Reviewed-By: Guy Bedford <[email protected]>
Fixes: #33741 PR-URL: #37852 Reviewed-By: Guy Bedford <[email protected]>
Fixes: #33741 PR-URL: #37852 Reviewed-By: Guy Bedford <[email protected]>
Not working for me |
There's an interesting problem with node checking package.json for package.json
...
"type": "module",
"scripts": {
"build": "npm run increment && ...",
"increment": "node scripts/increment_version.js",
... Then node throws the error "ReferenceError: require is not defined" inside the scripts/increment_version.js. Removing Is there a work-around for that? If not, then please reopen this issue to fix for the normal use of js scripts in package build time. |
@iva2k either name the script |
@ljharb - not my package, it is existing github repo I cloned and plan PR to add a feature. Changing whole build setup (which involves CI and CD) and renaming all files in the package is very steep price to pay. Is there a better fix, e.g. I'm pondering to add package.json file without "type: module" to scripts/ dir. |
@iva2k that's another option (specifically, you'd need |
It turns out an empty {} package.json in scripts/ solved that particular problem. I added "type: common" to be more specific, and that is clean enough workaround for this situation, works too. Anyhow, it seems too far-fetched conclusion that node makes based on package.json contents. What is its rationale? |
I jumped to conclusion too early that stub package.json works. Now
Throwing package.json files around node_modules tree is not an easy workaround. |
|
webpack-cli does not seem to find .cjs config file. So the summary: |
unfortunately type module causes nothing but problems ¯\_(ツ)_/¯ |
so module npm packages are a bad idea what is the node's changelog that brought this change? |
The rationale was to allow users to write ES modules using For your particular issue, here's the solutions that could work:
|
who's is sending
me all this?
…On Thu, Jul 29, 2021 at 6:04 AM Antoine du Hamel ***@***.***> wrote:
The rationale was to allow users to write ES modules using .js as file
extension. In the browser, we can use <script type="module"> to tell the
JS engine to parse the file as an ES module rather than a script, it was
decided that using package.json would be the Node.js equivalent of that.
For your particular issue, here's the solutions that could work:
- Remove "type": "module" from the package.json and use the .mjs
extension for all ES modules as @ljharb <https://github.com/ljharb>
suggested.
- Rename the Webpack config to webpack.config.cjs and create a
webpack.config.js that simply contains export { default } from
'./webpack.config.cjs';
- Convert your webpack.config.js to an ES module by hand. You might be
able to do that by replacing module.exports with export default and
adding this snippet at the top of the file:
import { createRequire } from 'module';import { fileURLToPath } from 'url';import { dirname } from 'path';const require = createRequire(import.meta.url);const __filename = fileURLToPath(import.meta.url);const __dirname = dirname(__filename);
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#33741 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AHSW5OVIAYO43HQCP7SUFRDT2ERRBANCNFSM4NTO7NIQ>
.
|
@reuben4310 you are getting the emails because you commented on this post here: #33741 (comment) @ArashAmini345 please open a new issue in the help repo https://github.com/nodejs/help/issues :) |
What steps will reproduce the bug?
console.log(require)
How often does it reproduce? Is there a required condition?
All the time.
What is the expected behavior?
The behavior should be similar to node executing the same one liner in REFL interactive shell.
What do you see instead?
$ node test.js
(node:13608) ExperimentalWarning: The ESM module loader is experimental. file:///somedirectory/test.js:1 console.log(require); ^ ReferenceError: require is not defined
Additional information
The issue does not happen in older node versions. See text output below
The text was updated successfully, but these errors were encountered: