-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Conversation
@@ -30,6 +30,7 @@ jobs: | |||
yarn add -D eslint-config-react-app eslint | |||
|
|||
yarn build | |||
yarn test |
There was a problem hiding this comment.
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.
const basename = parentPath && path.basename(filename) === | ||
path.basename(parentPath) ? filename : path.basename(filename); |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
6a76e27
to
cbe79ed
Compare
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? |
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 🤔 |
As long as projects use |
Yes, the problem is non-js tooling like esbuild that is more common recently. |
Next.js fallbacks to the Node API when it detects the path matches a pattern. |
What's the problem this PR addresses?
Calling
require
on an ES Module throwsSyntaxError: Cannot use import statement outside a module
instead ofERR_REQUIRE_ESM
Fixes #4016
How did you fix it?
If the
.js
file is in a module context (package.json
containstype: module
) throwERR_REQUIRE_ESM
.Checklist