nodejs / node

Node.js JavaScript runtime ✨🐢🚀✨
https://nodejs.org
Other
105.17k stars 28.49k forks source link

[Feature request] self-referencing should work without `"exports"` #50334

Open trusktr opened 8 months ago

trusktr commented 8 months ago

Version

v20.5.0

Platform

Microsoft Windows NT 10.0.19045.0 x64

Subsystem

npm 10.2.0

What steps will reproduce the bug?

This build shows the problem:

https://github.com/lume/kiwi/actions/runs/6609081452

This commit:

https://github.com/lume/kiwi/commit/45fa46609094b75c95d26252c3899ea3cbcfe048

To reproduce the error:

git clone git@github.com:lume/kiwi.git
cd kiwi
git checkout 45fa46609094b75c95d26252c3899ea3cbcfe048
npm install
npm run bench

Output after that will be:

Error [ERR_MODULE_NOT_FOUND]: Cannot find package '@lume/kiwi' imported from /home/runner/work/kiwi/kiwi/bench/main.js
    at new NodeError (node:internal/errors:372:5)
    at packageResolve (node:internal/modules/esm/resolve:908:9)
    at moduleResolve (node:internal/modules/esm/resolve:957:20)
    at defaultResolve (node:internal/modules/esm/resolve:1173:11)
    at ESMLoader.resolve (node:internal/modules/esm/loader:[60](https://github.com/lume/kiwi/actions/runs/6609081452/job/17948697361#step:4:61)4:30)
    at ESMLoader.getModuleJob (node:internal/modules/esm/loader:318:18)
    at ESMLoader.import (node:internal/modules/esm/loader:404:22)
    at importModuleDynamically (node:internal/modules/esm/translators:106:35)
    at importModuleDynamicallyCallback (node:internal/process/esm_loader:35:14)
    at main (file:///home/runner/work/kiwi/kiwi/bench/main.js:12:15) {
  code: 'ERR_MODULE_NOT_FOUND'

How often does it reproduce? Is there a required condition?

Every time

What is the expected behavior? Why is that the expected behavior?

Self-referencing imports should work

What do you see instead?

it fails

Additional information

The issue can be temporarily worked around by making a symlink, like so:

cd node_modules
mkdir @lume
cd @lume
ln -s ../../ kiwi

After this, node_modules/@lume/kiwi will be a link back up to kiwi itself, and npm run bench will work.

targos commented 8 months ago

https://nodejs.org/dist/latest-v21.x/docs/api/packages.html#self-referencing-a-package-using-its-name

Self-referencing is available only if package.json has "exports", and will allow importing only what that "exports" (in the package.json) allows.

trusktr commented 8 months ago

Ah, thanks. That's not great, considering that exports is optional and other packages can import the package fine, so one would think the package should be able to intuitively import itself fine too.

I'd like to avoid having extras that I don't need, like exports, staying closer to vanilla ESM.

trusktr commented 8 months ago

Is it viable to make this a feature request? Or is there a complication with allowing it without exports?

targos commented 8 months ago

@nodejs/loaders

ljharb commented 8 months ago

exports is only optional because there’s no backwards compatible way to add it by default - every package should have it.

“vanilla ESM” works a similar way in the sense that you choose which files to deploy, and thus which are exposed to the web.

Self-reference uses “exports” paths, so I’m not sure how it’d make sense to have it without exports being present.

GeoffreyBooth commented 8 months ago

I think you should be able to create an exports field and just expose everything?

"exports": {
  ".": "./dist/kiwi.js",
  "./*": "./*"
}
github-actions[bot] commented 3 days ago

There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the https://github.com/nodejs/node/labels/never-stale label or close this issue if it should be closed. If not, the issue will be automatically closed 6 months after the last non-automated comment. For more information on how the project manages feature requests, please consult the feature request management document.