-
-
Notifications
You must be signed in to change notification settings - Fork 4
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 package.json to exports #1
Conversation
Signed-off-by: Andrew Branch <[email protected]>
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.
All packages need to provide this, or tooling breaks.
Heya! I dunno, I don’t feel I have personally been doing things like: import fs from 'node:fs/promises'
const pkgUrl = new URL('package.json', import.meta.resolve('bare-specifier'))
const pkgJson = JSON.parse(String(await fs.readFile(pkgUrl)))
console.log(pkgJson) I’m pragmatic about it though, if you need it we can add it!
I have 100s of packages without |
That technique can’t work 100% of the time; the bare specifier doesn’t necessarily resolve to the package directory. |
It was an example, a variable, |
The former does always work prior to exports, which is why adding it in explicitly is helpful - because it’s what everything assumes will work. |
I have no clue what you mean. |
The point of the exports field is just to make explicit which files are accessible, which isn’t the same thing as defining your API. Prior to that field existing, you could always require bare-specifier/package.json, so all tools for the first decade of node have assumed that will work, and used it over fs APIs. With the advent of the exports field, there’s no longer a reliable way that works for every package to get at the package.json - so, if you add it to exports as this PR does, then you’ve restored the path that every tool already assumes will work, and there’s no other reliable workaround anyways (ie, no official “get the package dir” node api) |
Right, the export field is different, I like these changes, that’s why I use it. |
So like, which automated tools does not having a |
In my mind, the API contract around exposing a package.json is limited to
And otherwise the content is not part of the API. But as far as I know, this wasn’t written down somewhere; it just feels reasonable to me. If you disagree, no worries; of course I can work around it in the way you showed. I opened the PR because my project already was using Happy to work with whatever you decide here. |
Why would |
Ah, whoops, sorry about that. I’ve been doing majors everywhere but I guess I forgot that here.
In this case it is. In other cases it isn’t. Such as this case where TS incorrectly thinks Does TS actually do things with JSON files? Assuming a |
When the tsconfig option is set (as init sets it), I believe it works just as it works with CJS. |
Thanks for the PR and the discussion! And sorry that I pushed this change in a minor instead of a major! |
@arethetypeswrong/history includes the npm-high-impact version used to generate its data, so being able to access the package.json via import/require is convenient 😄