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

Error ERR_REQUIRE_ESM on attempt to use a ES module with *.js extension as a processor #3203

Open
drdaemos opened this issue Jun 17, 2024 · 0 comments

Comments

@drdaemos
Copy link

Version info:

Artillery: 2.0.14
Node.js:   v20.9.0
OS:        linux

Running this command:

artillery run test.yml

I expected test to run successfully, but
Instead, this happened:

⠋ /<...>/node_modules/@artilleryio/int-core/lib/runner.js:97
      script.config.processor = require(processorPath);
                                ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /<...>/processor.js from /<...>/node_modules/@artilleryio/int-core/lib/runner.js not supported.
Instead change the require of processor.js in /<...>/node_modules/@artilleryio/int-core/lib/runner.js to a dynamic import() which is available in all CommonJS modules.
    at loadProcessor (/<...>/node_modules/@artilleryio/int-core/lib/runner.js:97:33)

Files being used:

[test.yml]

config:
  # ...
  processor: 'processor.js' # pointed to a ES Module

My project has ES modules with *.js extension and type="module" set in the package.json. According to https://nodejs.org/api/packages.html#determining-module-system it is a valid way of specifying ES module files.

Problem arises from the following code in packages/core/lib/runner.js:

async function loadProcessor(script, options) {
  const absoluteScriptPath = path.resolve(process.cwd(), options.scriptPath);
  if (script.config.processor) {
    const processorPath = path.resolve(
      path.dirname(absoluteScriptPath),
      script.config.processor
    );

    if (processorPath.endsWith('.mjs')) { // <- this check is causing to divert to incorrect path
      const exports = await import(processorPath);
      script.config.processor = Object.assign(
        {},
        script.config.processor,
        exports
      );
    } else {
      // CJS (possibly transplied from TS)
      script.config.processor = require(processorPath);
    }
  }

  return script;
}

Probable fix could be simply removing the conditional and always attempt to import(module) as Node.js should be able to determine the module format and import it correctly for both CJS and ESM.

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

No branches or pull requests

1 participant