-
-
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): load the entrypoint as CJS if it isn't ESM #3832
Conversation
70a8f61
to
a7939c9
Compare
I do want to point out that this doesn't resolve some bad behavior that won't be possible to fix without an implementation of this comment: If/when this lands, we can restore completely correct behavior by gating off my /* project has { "type": "module" } */
import m from './some-cjs-bin-script'; // should throw with ERR_UNKNOWN_FILE_EXTENSION ""
console.log(m); // [Module: null prototype] ... |
You can use |
It doesn't look like $ yarn node entrypoint.mjs
[Arguments] {
'0': 'file:///Users/herockk/Workspaces/yarn-esm-loader-entrypoint/entrypoint.mjs',
'1': { format: undefined },
'2': [AsyncFunction: defaultLoad]
}
[Arguments] {
'0': 'file:///Users/herockk/Workspaces/yarn-esm-loader-entrypoint/commonjs',
'1': { format: undefined },
'2': [AsyncFunction: defaultLoad]
}
[Arguments] {
'0': 'file:///Users/herockk/Workspaces/yarn-esm-loader-entrypoint/test.js',
'1': { format: undefined },
'2': [AsyncFunction: defaultLoad]
} However, this approach looks viable to me: https://twitter.com/Rich_Harris/status/1355289863130673153 In my unix environments, the following seems to work function getFileFormat(filepath: string): string | null {
const isMain = process.argv[1] === filepath;
// ...
} |
a7939c9
to
88b686c
Compare
It's only there for the |
|
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.
LGTM, thanks for fixing this 👍
Tests are failing because the import causes an unhandled promise rejection, which is associated with exit code 0 in Node <15 |
2035186
to
a757d96
Compare
It appears this might not be the complete fix for this problem. I've been suddenly getting this error with Yarn canary during the installation and packing of a git dependency. Ref: https://github.com/andreialecu/nestjs-sentry/blob/aa/refactor/package.json#L22
This is a regression, because reverting to yarn 3.1.1 fixes it. I also tried Note that the More context at: https://discord.com/channels/226791405589233664/654372321225605128/917853587642671154 |
Make sure that yarn add @ntegral/nestjs-sentry@andreialecu/nestjs-sentry with 3.2.0-rc.7 and it exited successfully. |
Here's a repro: corepack enable npm
cd `mktemp -d`
yarn init -2
yarn set version from sources
yarn config set pnpEnableEsmLoader true
yarn
NODE_OPTIONS="--loader $PWD/.pnp.loader.mjs" yarn add test@andreialecu/nestjs-sentry#aa/refactor
Note that in my actual project I'm not adding |
Above issue still exists on latest Opened #3900 |
What's the problem this PR addresses?
Resolves #3782.
How did you fix it?
I carefully noted differences in how Node behaves with or without
--experimental-loader
with respect to loading a CJS entrypoint. The goal of this PR is to preserve the behavior that Node has when no--experimental-loader
flag is passed to the entrypoint script. The behavior introduced in #3667 is missing the following:{ "type": "module" }
in package.json[.js, .cjs, .mjs]
(and[.json, .wasm]
under certain circumstances) should fail to load with{ "type": "module" }
{ "type": "module" }
, any non-.mjs
file should load as CJS.Checklist