Skip to content

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

Merged
aduh95 merged 3 commits into
mainfrom
pkg-json-exports
Feb 23, 2023
Merged

aduh95 merged 3 commits into
mainfrom
pkg-json-exports

Conversation

@aduh95

@aduh95 aduh95 commented Feb 19, 2023

Copy link
Copy Markdown
Contributor

…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

merceyz commented Feb 19, 2023

Copy link
Copy Markdown
Member

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

ljharb commented Feb 19, 2023

Copy link
Copy Markdown
Member

Note that adding exports is a breaking change.

@aduh95

aduh95 commented Feb 22, 2023

Copy link
Copy Markdown
Contributor Author

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.

Comment thread tests/_runCli.ts Outdated
@aduh95
aduh95 merged commit 8e12d08 into main Feb 23, 2023
@aduh95
aduh95 deleted the pkg-json-exports branch February 23, 2023 09:15
@merceyz

merceyz commented Feb 23, 2023

Copy link
Copy Markdown
Member

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