Skip to content
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

[Unit tests] ReferenceError: require is not defined in ES module scope #6240

Closed
dsogari opened this issue Jan 25, 2024 · 8 comments · Fixed by #6245
Closed

[Unit tests] ReferenceError: require is not defined in ES module scope #6240

dsogari opened this issue Jan 25, 2024 · 8 comments · Fixed by #6245

Comments

@dsogari
Copy link

dsogari commented Jan 25, 2024

Hello there. I'm doing a build of binaryen as a dependency of an npm package. Running check.py fails because the test/unit/input/asyncify.js cannot be run by nodejs when the nearest parent directory which contains package.json is configured with "type": "module". The error message from executing it directly is:

<redacted>/binaryen/test/unit/input/asyncify.js:6
var fs = require('fs');
         ^

ReferenceError: require is not defined in ES module scope, you can use import instead
This file is being treated as an ES module because it has a '.js' file extension and '<redacted>/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension.
    at file:///<redacted>/binaryen/test/unit/input/asyncify.js:6:10
    at ModuleJob.run (node:internal/modules/esm/module_job:218:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:329:24)
    at async loadESM (node:internal/process/esm_loader:28:7)
    at async handleMainPromise (node:internal/modules/run_main:113:12)

The solution is either adding a package.json with an empty configuration ({}) to the test/unit/input directory or changing the script extension to .cjs.

@kripken
Copy link
Member

kripken commented Jan 25, 2024

Which node.js version are you using? I think I saw this before and it depends on the node version, but I don't remember if newer or older versions show the error.

@dsogari
Copy link
Author

dsogari commented Jan 25, 2024

I'm using v20.11.0

@kripken
Copy link
Member

kripken commented Jan 25, 2024

Thanks, I can't reproduce the issue though:

NODE=~/Downloads/node-v20.11.0-linux-x64/bin/node ./check.py unit

That runs all the unit tests with node 20.11, and they pass.

Which test specifically fails for you?

@dsogari
Copy link
Author

dsogari commented Jan 25, 2024

Just for clarity, in your test did you add a package.json file with {"type": "module"} in a parent folder?

@kripken
Copy link
Member

kripken commented Jan 25, 2024

No, I was trying to first see the error before seeing if that fixes it.

To make sure I verified there is no such file in the directory, nor any parent.

@dsogari
Copy link
Author

dsogari commented Jan 25, 2024

No, what I mean is, to reproduce this error, there should be package.json in a parent directory with type module (which is what my project is). Sorry for the misunderstanding.

I edited the original issue comment to be a little more precise about this fact.

kripken added a commit to kripken/binaryen that referenced this issue Jan 25, 2024
@kripken
Copy link
Member

kripken commented Jan 25, 2024

I see, apologies for my confusion. That was probably obvious to someone that knows more JS, unlike me 😄

I can reproduce and see the proposed fix works. Opened #6245

@dsogari
Copy link
Author

dsogari commented Jan 25, 2024

No worries, I'm also a JS newcomer. 😄

Thanks for your quick feedback and resolution!

kripken added a commit that referenced this issue Feb 8, 2024
The JS there is not an ES6 module, so declare it so (otherwise a package.json in a
parent, perhaps in folders outside of our own project that we are pasted in, can
cause an error, as require does not work in ES6 modules and we might be forced
to be seen as one).

Fixes #6240
radekdoulik pushed a commit to dotnet/binaryen that referenced this issue Jul 12, 2024
The JS there is not an ES6 module, so declare it so (otherwise a package.json in a
parent, perhaps in folders outside of our own project that we are pasted in, can
cause an error, as require does not work in ES6 modules and we might be forced
to be seen as one).

Fixes WebAssembly#6240
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants