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

fix(pnp): throw ERR_REQUIRE_ESM when requiring an ES Module #4024

Merged
merged 5 commits into from Jan 27, 2022

Conversation

merceyz
Copy link
Member

@merceyz merceyz commented Jan 26, 2022

What's the problem this PR addresses?

Calling require on an ES Module throws SyntaxError: Cannot use import statement outside a module instead of ERR_REQUIRE_ESM

Fixes #4016

How did you fix it?

If the .js file is in a module context (package.json contains type: module) throw ERR_REQUIRE_ESM.

Checklist

  • I have read the Contributing Guide.
  • I have set the packages that need to be released for my changes to be effective.
  • I will check that all automated PR checks pass before the PR gets reviewed.

@merceyz merceyz requested a review from arcanis as a code owner January 26, 2022 21:50
@@ -30,6 +30,7 @@ jobs:
yarn add -D eslint-config-react-app eslint

yarn build
yarn test
Copy link
Member Author

@merceyz merceyz Jan 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fails without this PR when run outside of the CI where file watchers are enabled.

packages/yarnpkg-pnp/sources/loader/nodeUtils.ts Outdated Show resolved Hide resolved
Comment on lines 45 to 46
const basename = parentPath && path.basename(filename) ===
path.basename(parentPath) ? filename : path.basename(filename);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's taken from the Node codebase but ending the line with === is quite an unusual pattern for ours.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, our linting/formatting setup should be updated to handle this.

@merceyz merceyz force-pushed the merceyz/fix/pnp-ERR_REQUIRE_ESM branch from 6a76e27 to cbe79ed Compare January 27, 2022 08:51
@arcanis arcanis merged commit ba821ef into master Jan 27, 2022
@arcanis arcanis deleted the merceyz/fix/pnp-ERR_REQUIRE_ESM branch January 27, 2022 10:54
@merceyz merceyz added the esm label Jan 27, 2022
@swandir
Copy link
Contributor

swandir commented Jan 27, 2022

I'm curious is this problem unique to cjs loader specifically?

Does this happen because Node.js can't locate corresponding package.json for an import resolved through PnP?

@swandir
Copy link
Contributor

swandir commented Jan 27, 2022

Thanks!

A somewhat similar issue affect bundlers, which too want to locate package.json (and maybe other configs) to handle advanced resolution.

In theory this kind of things should be handled via plugins providing custom resolution, but it can be tricky (vitejs/vite#6493 (comment)) in practice.

I guess per-module resolution hooks are not completely sufficient abstraction for implementing PnP support?

I wonder whether the best approach is to try handling such edge cases in pnp bundler plugins, or rather to add an ability to read zip archives into a bundler 🤔

@merceyz
Copy link
Member Author

merceyz commented Jan 27, 2022

As long as projects use require.resolve and fs to interact with dependencies it should just work out of the box

@swandir
Copy link
Contributor

swandir commented Jan 27, 2022

Yes, the problem is non-js tooling like esbuild that is more common recently.

@arcanis
Copy link
Member

arcanis commented Jan 27, 2022

Next.js fallbacks to the Node API when it detects the path matches a pattern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug?]: CJS loader error caught in PnP is different from expected
4 participants