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

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

Open
trusktr opened this issue Oct 23, 2023 · 7 comments
Open

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

trusktr opened this issue Oct 23, 2023 · 7 comments
Labels
feature request Issues that request new features to be added to Node.js. module Issues and PRs related to the module subsystem. stale

Comments

@trusktr
Copy link
Contributor

trusktr commented Oct 23, 2023

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:

lume/kiwi@45fa466

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 targos added the module Issues and PRs related to the module subsystem. label Oct 23, 2023
@targos
Copy link
Member

targos commented Oct 23, 2023

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
Copy link
Contributor Author

trusktr commented Oct 23, 2023

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
Copy link
Contributor Author

trusktr commented Oct 23, 2023

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

@targos
Copy link
Member

targos commented Oct 23, 2023

@nodejs/loaders

@ljharb
Copy link
Member

ljharb commented Oct 23, 2023

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
Copy link
Member

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

"exports": {
  ".": "./dist/kiwi.js",
  "./*": "./*"
}
@aduh95 aduh95 changed the title self-referencing import is not working Jan 4, 2024
@aduh95 aduh95 added the feature request Issues that request new features to be added to Node.js. label Jan 4, 2024
Copy link
Contributor

github-actions bot commented Jul 3, 2024

There has been no activity on this feature request for 5 months. To help maintain relevant open issues, please add the never-stale Mark issue so that it is never considered 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.

@github-actions github-actions bot added the stale label Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request Issues that request new features to be added to Node.js. module Issues and PRs related to the module subsystem. stale
5 participants