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

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

Open
1 task
blutorange opened this issue Nov 12, 2024 · 5 comments · May be fixed by #6596
Open
1 task
Labels
bug Something isn't working

Comments

@blutorange
Copy link

blutorange commented Nov 12, 2024

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 ?

@blutorange blutorange added the bug Something isn't working label Nov 12, 2024
@RDIL RDIL linked a pull request Nov 12, 2024 that will close this issue
3 tasks
@RDIL
Copy link
Member

RDIL commented Nov 12, 2024

Hey @blutorange, can you please see if running yarn set version from sources --branch "reece/fix-mts" and then re-testing it resolves the issue?

@blutorange
Copy link
Author

blutorange commented Nov 13, 2024

@RDIL It didn't, but I saw your commit c38fd4e and noticed that the change wasn't applied to .pnp.loader.mjs, even after I deleted that file and had yarn recreate it. I manually added your change regarding getFileFormat to my local copy of .pnp.loader.mjs. Then it worked : ) Probably just an issue with installing from sources.

So yes, that commit should be enough to fix it. It's probably best to also add .cts for commonjs

@RDIL
Copy link
Member

RDIL commented Nov 13, 2024

Whoops, I forgot to rebuild the loader, my bad. Will also add CTS!

@goloveychuk
Copy link
Contributor

goloveychuk commented Dec 16, 2024

@RDIL adding

case `.mjs`: {
      return `module`;
    }

to pnp loader fixes the issue, can you merge your PR? Thanks

@goloveychuk
Copy link
Contributor

goloveychuk commented Dec 16, 2024

BUT, interestingly, strip-types argument does not work.
Image
this "fixes.
Image
Seems like stripping types is also done in loader, so you need to chain it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants