Skip to content

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

Description

@dsogari

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions