Skip to content

[Bug?]: ERR_REQUIRE_ESM in Yarn PnP mode using Node.js 22 with *.mts file and --experimental-strip-types flag #6595

@blutorange

Description

@blutorange

Self-service

  • I'd be willing to implement a fix

Describe the bug

Trying to run a *.mt file via yarn node with no type or type: "commonjs" in the package.json fails and results in an ERR_REQUIRE_ESM error.:

node:internal/modules/cjs/loader:1314
      throw new ERR_REQUIRE_ESM(filename, true);
            ^

Error [ERR_REQUIRE_ESM]: require() of ES Module /home/user/git/project/a.mts not supported.
    at require$$0.Module._load (/home/user/git/project/.pnp.cjs:6014:31)
    at TracingChannel.traceSync (node:diagnostics_channel:315:14) {
  code: 'ERR_REQUIRE_ESM'
}

Judging from the error message require() of ES Module and .pnp.cjs. I'm guessing yarn currently just doesn't implement the logic for treating *.mts files as ESM and uses its .pnp.cjs instead of .pnp.loader.mjs ?

To reproduce

Make sure the package.json either has no type or "type": "commonjs".

Create two files:

// a.mts
import Message from "./b.mts";
console.log(Message);

// b.mts
const Hello = "World";
export default Hello;

Then run yarn node --experimental-strip-types a.mts. This will fail with an error ERR_REQUIRE_ESM'

Running node --experimental-strip-types a.mts works and prints World.

Environment

System:
    OS: Linux 6.8 Ubuntu 24.04.1 LTS 24.04.1 LTS (Noble Numbat)
    CPU: (8) x64 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  Binaries:
    Node: 22.11.0 - /tmp/xfs-ca62210f/node
    Yarn: 4.5.1 - /tmp/xfs-ca62210f/yarn
    npm: 10.9.0 - ~/.nvm/versions/node/v22.11.0/bin/npm

Additional context

Perhaps related to #6336 ?

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions