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
Changes from 3 commits
a8ec1b0
e1a8bfe
777fbcc
62e2a24
cbe79ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
releases: | ||
"@yarnpkg/cli": patch | ||
"@yarnpkg/plugin-pnp": patch | ||
"@yarnpkg/pnp": patch | ||
|
||
declined: | ||
- "@yarnpkg/esbuild-plugin-pnp" | ||
- "@yarnpkg/plugin-compat" | ||
- "@yarnpkg/plugin-constraints" | ||
- "@yarnpkg/plugin-dlx" | ||
- "@yarnpkg/plugin-essentials" | ||
- "@yarnpkg/plugin-init" | ||
- "@yarnpkg/plugin-interactive-tools" | ||
- "@yarnpkg/plugin-nm" | ||
- "@yarnpkg/plugin-npm-cli" | ||
- "@yarnpkg/plugin-pack" | ||
- "@yarnpkg/plugin-patch" | ||
- "@yarnpkg/plugin-pnpm" | ||
- "@yarnpkg/plugin-stage" | ||
- "@yarnpkg/plugin-typescript" | ||
- "@yarnpkg/plugin-version" | ||
- "@yarnpkg/plugin-workspace-tools" | ||
- "@yarnpkg/builder" | ||
- "@yarnpkg/core" | ||
- "@yarnpkg/doctor" | ||
- "@yarnpkg/nm" | ||
- "@yarnpkg/pnpify" | ||
- "@yarnpkg/sdks" |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import 'fs' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"name": "no-deps-esm", | ||
"version": "1.0.0", | ||
"type": "module" | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import 'fs' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "no-deps-mjs", | ||
"version": "1.0.0" | ||
} |
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
import {NativePath, npath} from '@yarnpkg/fslib'; | ||
import fs from 'fs'; | ||
import {Module} from 'module'; | ||
import path from 'path'; | ||
|
||
// @ts-expect-error | ||
const builtinModules = new Set(Module.builtinModules || Object.keys(process.binding(`natives`))); | ||
|
@@ -36,3 +37,20 @@ export function readPackage(requestPath: NativePath) { | |
|
||
return JSON.parse(fs.readFileSync(jsonPath, `utf8`)); | ||
} | ||
|
||
// https://github.com/nodejs/node/blob/972d9218559877f7fff4bb6086afacac8933f8d1/lib/internal/errors.js#L1450-L1478 | ||
// Our error isn't as detailed since we don't have access to acorn to check | ||
// if the file contains ESM syntax | ||
export function ERR_REQUIRE_ESM(filename: string, parentPath: string | null = null) { | ||
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 commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, our linting/formatting setup should be updated to handle this. |
||
|
||
let msg = `require() of ES Module ${filename}${parentPath ? ` from ${ | ||
parentPath}` : ``} not supported.`; | ||
msg += `\nInstead change the require of ${basename} in ${parentPath} to` + | ||
` a dynamic import() which is available in all CommonJS modules.`; | ||
merceyz marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
const err = new Error(msg) as Error & {code: string}; | ||
err.code = `ERR_REQUIRE_ESM`; | ||
return err; | ||
} |
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.