question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can not resolve when package.json exports field resolution result is a direcory

See original GitHub issue

Describe the bug

I have a package named some-package and it’s files are in node_modules.

package.json in node_modules/some-package

{
  "name": "some-package",
  "version": "1.0.0",
  "license": "MIT",
  "exports": {
    ".": "./index.js",
    "./sub": "./lib/sub"
  }
}

index.js in node_modules/some-packages/lib/sub

export const Foo = 'Foo';

then I import it in the main.js in root directory

import { Foo } from 'some-package/sub';
console.log(Foo);

it complain the error image

I also run the same code in Node.js or Webpack-based project, they can correctly resolve some-package/sub

In previous issue #7267 I know how to fix it. I’m willing to submit a PR with well-tested code❤️ if Vite team think it’s a bug

Reproduction

https://stackblitz.com/edit/vitejs-vite-cz6tvn?file=main.js

System Info

I don't know the system info in Stackbliz. But this issue is not platform-related I think

Used Package Manager

npm

Logs

No response

Validations

Issue Analytics

  • State:open
  • Created 2 years ago
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
sapphi-redcommented, Jun 9, 2022

Do not rely on this flag. We plan to remove it once the Loaders API has advanced to the point that equivalent functionality can be achieved via custom loaders. https://nodejs.org/api/esm.html#customizing-esm-specifier-resolution-algorithm

It seems --experimental-specifier-resolution=node is planed to be removed in future. It might be better to think of supporting custom loaders rather than this flag specifically.

IMHO relying on a flag / custom loader is not good for packages.

1reaction
3lang3commented, Apr 1, 2022

some issue.

nextjs remix wepack(cra, umi) resolve success.

only vite throw err

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to resolve modules specified in package.json's " ...
I'm working on a JS web application and am trying to use the exports field in a package.json file to clean up the...
Read more >
https://nodejs.org/api/modules.md
If X does not match this pattern or DIR/NAME/package.json is not a file, return. 3. Parse DIR/NAME/package.json, and look for "exports" field. 4....
Read more >
rollup.js
If your package.json file also has a module field, ES-module-aware tools like ... rollup.config.js // can be an array (for multiple inputs) export...
Read more >
Package exports
The exports field in the package.json of a package allows to declare which module ... Note: Only the first valid result will be...
Read more >
📔 Module Resolution
~/foo resolves foo relative to nearest node_modules directory, the nearest directory with package.json or the project root - whichever comes first.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found