Skip to content

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

@drdaemos

Description

@drdaemos

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.

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