Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Vite unable to resolve a dependency that node solves, even when index is added to the path #14769

Closed
ziedHamdi opened this issue Oct 26, 2023 · 1 comment

Comments

@ziedHamdi
Copy link

ziedHamdi commented Oct 26, 2023

Describe the bug

I checked this error on the web and specifically for vite, and although it seems like a common discussion, I don't understand why it doesn't work on my case:

I published the lib user-credits on npm and while it resolves in regular node apps it cannot be resolved through my svelte app.

"dependencies": {
    "@sveltejs/adapter-node": "1.3.1",
    "@tsconfig/svelte": "^5.0.2",
    "preline": "^1.9.0",
    "user-credits": "0.9.7-alpha"
}

Now when I launch the app (vite dev), I get:

Directory import 'C:\Users\zhamd\work\user-credits-ui\node_modules\.pnpm\user-credits@0.9.7-alpha\node_modules\user-credits\dist\db\dao' is not supported resolving ES modules imported from C:\Users\zhamd\work\user-credits-ui\node_modules\.pnpm\user-credits@0.9.7-alpha\node_modules\user-credits\dist\index.js
Error [ERR_UNSUPPORTED_DIR_IMPORT]: Directory import 'C:\Users\zhamd\work\user-credits-ui\node_modules\.pnpm\user-credits@0.9.7-alpha\node_modules\user-credits\dist\db\dao' is not supported resolving ES modules imported from C:\Users\zhamd\work\user-credits-ui\node_modules\.pnpm\user-credits@0.9.7-alpha\node_modules\user-credits\dist\index.js
    at new NodeError (node:internal/errors:399:5)
    at finalizeResolution (node:internal/modules/esm/resolve:319:17)
    at moduleResolve (node:internal/modules/esm/resolve:945:10)

So I went to my package.json and I pointed it to the directory unpacked from npm like follows:

"dependencies": {
"@sveltejs/adapter-node": "1.3.1",
"@tsconfig/svelte": "^5.0.2",
"preline": "^1.9.0",
"user-credits": "link:..\user-credits-ui\node_modules\.pnpm\user-credits@0.9.7-alpha\node_modules\user-credits"
}

Surprise! Everything works fine!

Can someone explain me how I am supposed to export the lib please?

Even when I explicitely added index to the folder imports, it still didn't resolve:

Cannot find module 'C:\Users\zhamd\work\user-credits-ui\node_modules.pnpm\user-credits@0.9.8-alpha\node_modules\user-credits\dist\db\dao\index' imported from C:\Users\zhamd\work\user-credits-ui\node_modules.pnpm\user-credits@0.9.8-alpha\node_modules\user-credits\dist\index.js

** Is it searching for an index.js or index.ts file? Because there's an index.js and and index.d.ts file at C:\Users\zhamd\work\user-credits-ui\node_modules.pnpm\user-credits@0.9.8-alpha\node_modules\user-credits\dist\db\dao\ **

NOTE: here's the problematic npm project's github and this is the client's one

In the file below, I tested that the same import works fine in a regular node app that i launch with node fileName.js
TestUserCredits.zip

Reproduction

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

Steps to reproduce

I wasn't able to see the logs on stackblitz, but commenting the two lines below in main.js shows the vite page, uncommenting the import will immediately show a blank page

  • import { setupCounter } from './counter.js';
  • setupCounter(document.querySelector('#counter'));

So just call

  • npm install && npm run dev --verbose

details:

I added the dependencies

  "dependencies": {
    "tsc": "^2.0.4",
    "typescript": "^5.2.2",
    "user-credits": "0.9.10-alpha"
  }

Modified counter.js

import { Offer } from './src/testImport.js';

export function setupCounter(element) {
  let counter = 0;
  const setCounter = (count) => {
    counter = count;
    element.innerHTML = `import successful ${JSON.stringify(
      new Offer('Hello', 'There'),
      null,
      '  '
    )}`;
  };
  element.addEventListener('click', () => setCounter(counter + 1));
  setCounter(0);
}

And implemented an interface from user-credits:

import {IOffer} from "user-credits";
import {OfferCycle} from "user-credits";

export class Offer implements IOffer<string> {
    _id: string = "123";
    customCycle: number | null = null;
    cycle: OfferCycle = "once";
    hasSubOffers: unknown = true;
    kind: "subscription" | "tokens" | "expertise" = "subscription";
    name: string;
    offerGroup: string;
    overridingKey: string = "";
    parentOfferId: string = "";
    popular: number = 0;
    price: number = 10;
    quantityLimit: number | null = null;
    tokenCount: number | null = null;
    weight: number = 2;

    constructor(name: string, offerGroup: string) {
        this.name = name;
        this.offerGroup = offerGroup;
    }

    markModified(field: string): void {
    }

    save(): Promise<unknown> {
        return Promise.resolve(undefined);
    }
}

the import will fail to find node_modules\user-credits\dist\db\dao

System Info

System:
    OS: Windows 10 10.0.19045
    CPU: (8) x64 Intel(R) Core(TM) i7-6700HQ CPU @ 2.60GHz
    Memory: 16.52 GB / 31.90 GB
  Binaries:
    Node: 18.16.0 - C:\Program Files\nodejs\node.EXE
    Yarn: 1.22.19 - ~\AppData\Roaming\npm\yarn.CMD
    npm: 9.5.1 - C:\Program Files\nodejs\npm.CMD
  Browsers:
    Edge: Spartan (44.19041.3570.0), Chromium (118.0.2088.69)
    Internet Explorer: 11.0.19041.3570

Used Package Manager

pnpm

Logs

Failed to load dependency ``` vite:import-analysis 6.60ms [5 imports rewritten] src\lib\example\impl\model\OfferProps.ts +6ms vite:transform 37.52ms /src/lib/example/impl/model/OfferProps.ts +7ms 5:32:06 PM [vite] Error when evaluating SSR module /src/lib/example/Resolver.ts: failed to import "user-credits" |- Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\zhamd\work\user-credits-ui\node_modules\.pnpm\user-credits@0.9.10-alpha\node_modules\user-credits\dist\db\dao\index' imported from C:\Users\zhamd\work\user-credits-ui\node_m odules\.pnpm\user-credits@0.9.10-alpha\node_modules\user-credits\dist\index.js at new NodeError (node:internal/errors:399:5) at finalizeResolution (node:internal/modules/esm/resolve:326:11) at moduleResolve (node:internal/modules/esm/resolve:945:10) at defaultResolve (node:internal/modules/esm/resolve:1153:11) at nextResolve (node:internal/modules/esm/loader:163:28) at ESMLoader.resolve (node:internal/modules/esm/loader:838:30) at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18) at ModuleWrap. (node:internal/modules/esm/module_job:77:40) at link (node:internal/modules/esm/module_job:76:36)

5:32:06 PM [vite] Error when evaluating SSR module /src/routes/+layout.svelte: failed to import "/src/lib/example/Resolver.ts"
|- Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\zhamd\work\user-credits-ui\node_modules.pnpm\user-credits@0.9.10-alpha\node_modules\user-credits\dist\db\dao\index' imported from C:\Users\zhamd\work\user-credits-ui\node_m
odules.pnpm\user-credits@0.9.10-alpha\node_modules\user-credits\dist\index.js
at new NodeError (node:internal/errors:399:5)
at finalizeResolution (node:internal/modules/esm/resolve:326:11)
at moduleResolve (node:internal/modules/esm/resolve:945:10)
at defaultResolve (node:internal/modules/esm/resolve:1153:11)
at nextResolve (node:internal/modules/esm/loader:163:28)
at ESMLoader.resolve (node:internal/modules/esm/loader:838:30)
at ESMLoader.getModuleJob (node:internal/modules/esm/loader:424:18)
at ModuleWrap. (node:internal/modules/esm/module_job:77:40)
at link (node:internal/modules/esm/module_job:76:36)

Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:\Users\zhamd\work\user-credits-ui\node_modules.pnpm\user-credits@0.9.10-alpha\node_modules\user-credits\dist\db\dao\index' imported from C:\Users\zhamd\work\user-credits-ui\node_modu
les.pnpm\user-credits@0.9.10-alpha\node_modules\user-credits\dist\index.js

</details>

### Validations

- [X] Follow our [Code of Conduct](https://github.com/vitejs/vite/blob/main/CODE_OF_CONDUCT.md)
- [X] Read the [Contributing Guidelines](https://github.com/vitejs/vite/blob/main/CONTRIBUTING.md).
- [X] Read the [docs](https://vitejs.dev/guide).
- [X] Check that there isn't [already an issue](https://github.com/vitejs/vite/issues) that reports the same bug to avoid creating a duplicate.
- [X] Make sure this is a Vite issue and not a framework-specific issue. For example, if it's a Vue SFC related bug, it should likely be reported to [vuejs/core](https://github.com/vuejs/core) instead.
- [X] Check that this is a concrete bug. For Q&A open a [GitHub Discussion](https://github.com/vitejs/vite/discussions) or join our [Discord Chat Server](https://chat.vitejs.dev/).
- [X] The provided reproduction is a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of the bug.
@stackblitz
Copy link

stackblitz bot commented Oct 26, 2023

Fix this issue in StackBlitz Codeflow Start a new pull request in StackBlitz Codeflow.

@vitejs vitejs locked and limited conversation to collaborators Oct 27, 2023
@bluwy bluwy converted this issue into discussion #14775 Oct 27, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Projects
None yet
Development

No branches or pull requests

1 participant