-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
ESM support: allow package self-reference imports #10883
Comments
Yeah, we should implement all Node does for ESM. This seems dependent on #9771 landing first, tho? |
Yep. No hurry, I have a workaround I'm using for now. Just wanted to ticket it so I would know when it was safe to remove the workaround. |
This also applies to CommonJS modules: https://nodejs.org/api/packages.html#packages_self_referencing_a_package_using_its_name |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
Is there any update on this? |
That is what I meant; that seems like a bug in node itself then. |
Why would it be?? The node docs say "If you don't plan to publish your package, the name and version fields are optional." I interpret that to say quite clearly that any directory containing a What you seem to be saying is that some people might doing things like configuring resolver behavior for particular subdirectories by defining a {
"type": "module"
} OK, well, nobody can stop people from doing that if it works well enough for them, but that doesn't mean it's supported. |
I even just tried putting something completely invalid in |
I'm saying exactly that - it's supported and it's what node's own docs recommend (i was on the node modules working group and was present for all those decisions) |
So what's the problem for self-refs then? If the nearest I didn't know you were part that @ljharb. I have mad respect for how much you've done, and I depend on your work for my livelihood several times over. |
I would expect, and hope, that node looks upwards until it finds a package boundary (a package.json next to the closest node_modules folder). |
Why would a My experimentation shows that only |
OK, let me try again :-) does node skip a name-less package.json when looking to resolve a self-reference? (The other question is: I believe self-reference respects "exports". But, node itself doesn't respect "exports" unless it's inside a node_modules package boundary. so i think that resolving a self-reference also requires "exports" support, which is why |
No, node never skips that I can tell. Copying from the node package.json#export docs (in case anyone else ever reads this):
My example repo uses only exports, and shows node to be respecting them even without the presence of I don't think it's an issue here though since the behavior for self-references is specified, and |
Sorry to be stuck on this point - so you're saying that self-references inside a package only work when there's not an intermediate package.json file between the self-reference and the package's actual root? (ie, a file with just "type" or "main", but no "name")? |
Yes, I am saying that. |
Why does that surprise you? It seems to me to be wholly consistent with the other decisions you said you were involved in making -- decisions which to me amount to saying it will always be safe and correct to do a simple search upwards for a |
It surprises me because "exports" was specifically designed to only be respected at package boundaries. From the outside of a non-package-boundary directory, you can't require or import the contents of that directory in a way that respects "exports" (unlike "main"). |
Well the moment you create a |
A package is something you can publish and put in another project's package.json - it's not demarcated merely by the presence of a package.json file. |
...so to sum up I'm not supposed to be able to make this |
How bad a problem is that? |
It will likely cause user confusion, and it makes things much much more complex for tooling (like jest, resolve, etc) for minimal benefit to the user. |
I was a bit wrong actually -- you can't even self-reference a package with no name. It's just one of those things that you shouldn't do and it's not clear why you would. Add it to all the other ways you can hurt yourself if you don't know what you're doing. As for "something you can publish and put in another project's package.json". Well, you don't have to have something publishable to have a usable "package", and in fact virtually anything can be put into another project's |
Lots of discussion here. 👍 Is the conclusion that for self reference, we can look to the closest package.json and if the name matches and there's Question: what happens if both a module in |
@SimenB That's my conclusion, and I'd think that self-refs would always have priority in the resolution process. If you meant to refer to your own package, you don't want changes in the runtime environment (e.g. additions to |
Yeah, playing in https://github.com/conartist6/node-self-ref-test I see
So to me logic look like
|
I don't think exports should be check, but instead allows It to work with main field too. |
Not from my testing, if https://nodejs.org/api/packages.html#self-referencing-a-package-using-its-name
|
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Node permits an es module inside a package to reference that package by its name. The behavior is documented here. I propose that jest also support this, as I believe the feature was created with testing in mind. It's highly useful for your internal tests to be able to reference your package in exactly the same way that external consumers will. I am willing to provide the implementation.
The text was updated successfully, but these errors were encountered: