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

Add "exports" to the package.json to make self-referencing import… #239

Merged
merged 3 commits into from
Feb 23, 2023

Conversation

aduh95
Copy link
Contributor

@aduh95 aduh95 commented Feb 19, 2023

…s work

I'm getting the following error, probably due to the switch to ESBuild (Webpack used to embed the JSON info in the JS file):

Internal Error: Cannot find module 'corepack/package.json'
Require stack:
- /Users/duhamean/Documents/node/deps/corepack/dist/corepack.js
Require stack:
- /Users/duhamean/Documents/node/deps/corepack/dist/corepack.js
    at Module._resolveFilename (node:internal/modules/cjs/loader:1060:15)
    at Function.resolve (node:internal/modules/helpers:118:19)
    at runVersion (…/node/deps/corepack/dist/corepack.js:44173:68)

This should fix it for me, but I'm unsure about others, maybe a better fix would be to embed the version string in the JS file? //cc @merceyz

@merceyz
Copy link
Member

merceyz commented Feb 19, 2023

I'm fine with this change, it works when distributed by Node.js since it's located in a node_modules folder but seems that isn't true before its packaged.

probably due to the switch to ESBuild

It should have been a problem before that as well due to this line but I guess you didn't hit that code path, with the move to esbuild I changed this hack which you do run into.

maybe a better fix would be to embed the version string in the JS file?

We (esbuild) already do that, this is used to get the path to the root of the package, not the version.

const manifestPath = require.resolve(`corepack/package.json`);
const distFolder = path.join(path.dirname(manifestPath), `dist`);

process.env.COREPACK_ROOT = path.dirname(require.resolve(`corepack/package.json`));

@ljharb
Copy link
Member

ljharb commented Feb 19, 2023

Note that adding exports is a breaking change.

@aduh95
Copy link
Contributor Author

aduh95 commented Feb 22, 2023

How does this work when Corepack is installed with the Node.js installer? I would expect this hack not to work,
unlike eval(__dirname) (at least I can see what it's doing).

Note that adding exports is a breaking change.

Thanks for the remainder, that wouldn't be a big deal to release Corepack 0.17.0.

tests/_runCli.ts Outdated Show resolved Hide resolved
@aduh95 aduh95 merged commit 8e12d08 into main Feb 23, 2023
@aduh95 aduh95 deleted the pkg-json-exports branch February 23, 2023 09:15
@merceyz
Copy link
Member

merceyz commented Feb 23, 2023

How does this work when Corepack is installed with the Node.js installer?

In that case Corepack is located in .../node_modules/corepack (#239 (comment)) so it can require itself, adding exports makes it work regardless of the path to Corepack due to https://github.com/nodejs/node/blob/0e3b796cc543c8e7c151f02462912592f7debd2d/lib/internal/modules/cjs/loader.js#L528.

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

Successfully merging this pull request may close these issues.

3 participants