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

Running on root directory "Failed to resolve entry for package" #6859

Closed
ruigomeseu opened this issue Feb 11, 2022 · 33 comments · Fixed by #14626
Closed

Running on root directory "Failed to resolve entry for package" #6859

ruigomeseu opened this issue Feb 11, 2022 · 33 comments · Fixed by #14626
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)

Comments

@ruigomeseu
Copy link

ruigomeseu commented Feb 11, 2022

Describe the bug

When running on Linux, in the root directory, if you try to access vite_url:3000/path-of-working-dir, you will get the following error:

Failed to resolve entry for package "/my-vue-app". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "/my-vue-app". The package may have incorrect main/module/exports specified in its package.json.

Steps to reproduce:

  1. cd /
  2. npm create vite@latest my-vue-app -- --template vue
  3. cd my-vue-app
  4. npm run dev
  5. Access http://192.168.64.13:3000/my-vue-app
  6. See error

If my-vue-app is on any other folder other than / (root directory) this error doesn't happen.

This is running on a Ubuntu VM inside my M1 Pro Macbook.

Reproduction

Reproduction steps above

System Info

System:
    OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
    CPU: (4) arm64 unknown
    Memory: 1.27 GB / 7.75 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.13.2 - /usr/bin/node
    Yarn: 1.22.15 - /usr/bin/yarn
    npm: 8.1.2 - /usr/bin/npm
  Browsers:
    Chromium: 98.0.4758.80
  npmPackages:
    @vitejs/plugin-vue: ^2.2.0 => 2.2.0
    vite: ^2.8.0 => 2.8.0

Used Package Manager

npm

Logs

Failed to resolve entry for package "/my-vue-app". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "/my-vue-app". The package may have incorrect main/module/exports specified in its package.json.
    at packageEntryFailure (/my-vue-app/node_modules/vite/dist/node/chunks/dep-c9c9d3e5.js:29511:11)
    at resolvePackageEntry (/my-vue-app/node_modules/vite/dist/node/chunks/dep-c9c9d3e5.js:29507:9)
    at tryResolveFile (/my-vue-app/node_modules/vite/dist/node/chunks/dep-c9c9d3e5.js:29237:38)
    at tryFsResolve (/my-vue-app/node_modules/vite/dist/node/chunks/dep-c9c9d3e5.js:29219:16)
    at Context.resolveId (/my-vue-app/node_modules/vite/dist/node/chunks/dep-c9c9d3e5.js:29112:56)
    at Object.resolveId (/my-vue-app/node_modules/vite/dist/node/chunks/dep-c9c9d3e5.js:38242:55)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at async ModuleGraph.resolveUrl (/my-vue-app/node_modules/vite/dist/node/chunks/dep-c9c9d3e5.js:53426:26)
    at async ModuleGraph.getModuleByUrl (/my-vue-app/node_modules/vite/dist/node/chunks/dep-c9c9d3e5.js:53311:23)
    at async doTransform (/my-vue-app/node_modules/vite/dist/node/chunks/dep-c9c9d3e5.js:52879:20


### 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 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.
@johnnyasantoss
Copy link

I've tested in a container running node:16-alpine interactively on a Linux host (5.14) and the bug is reproducible.

$ docker run --rm -it node:16-alpine ash
# npm create vite@latest my-vue-app -- --template vue
# cd /my-vue-app
# yarn
# yarn run dev --host

Then on a browser:
-> http://172.17.0.2:3000/my-vue-app -> Error 500

@daliborgogic
Copy link
Contributor

[vite] Internal server error: Failed to resolve entry for package...

Workaround; Entry point differs from index.js in root folder. Add your entrypoint to package.json main key:

{
  "main": "[your_entry_point]"
}

@mquandalle
Copy link

I encounter this issue with vite 2.9+ and it wasn't the case before. Defining an entry point in the “main” field of package.json indeed solve the issue.

But since I'm already defining my two entry points in optimizeDeps.entries I think it shouldn't be necessary to include them as well in the package.json

@saurabhdaware
Copy link
Contributor

Same. Started facing this after 2.9. I was able to reproduce it here - https://stackblitz.com/edit/github-12zodn?file=src%2Fentry-server.js&terminal=dev

For me, It seems to be happening when I use vite.transformIndexHtml and pass <style> tags to it during SSR 🤔

@mquandalle
Copy link

@saurabhdaware My issue was indeed caused by transformIndexHtml and I was able to fix it in betagouv/mon-entreprise@c2014f7

You probably don't have the exact same problem but you can log req.url to see what is the request causing this error.

@saurabhdaware
Copy link
Contributor

I think #7869 will resolve this right @poyoho? (Ref: #6859 (comment))

I am guessing the issue is happening because transformIndexHtml was rendering a <script> tag with a non-existent src 🤔

@saurabhdaware
Copy link
Contributor

You probably don't have the exact same problem but you can log req.url to see what is the request causing this error.

Yeah I think my issue is a bit different. Due to #7913 my code is rendering a non-existent src in <script>. Vite then probably thinks that it is my entry file and fails since it doesn't exist.

@poyoho
Copy link
Member

poyoho commented Apr 26, 2022

hello, is same with #7913 ?

@poyoho
Copy link
Member

poyoho commented Apr 26, 2022

after this PR, inline style will keep. and add a request for hmr. I test it in vite playground, it seem to work well. could you like to test in your case?

@saurabhdaware
Copy link
Contributor

hello, is same with #7913 ?

It is a bit different. The issue I mentioned in comment here #6859 (comment) seems to be because of the inlined CSS change however the cause for the original issue might be different.

But I don't think it will resolve this issue since it seems to be created before 2.9 release. The reason for original issue is probably different.

after this PR, inline style will keep. and add a request for hmr. I test it in vite playground, it seem to work well. could you like to test in your case?

Yup I'll check this

@saurabhdaware
Copy link
Contributor

Just checked locally for my use-case. Your PR did fix the #7913 but it did not fix this issue for me. This might be something different.

@poyoho
Copy link
Member

poyoho commented Apr 26, 2022

I noted. Is may all request process into transformIndex, error handle ?html-proxy&1.css

@AshenBird
Copy link

I don't konw . Is same problem cause of this ?

@AshenBird
Copy link

If I create a package at root directory , and package directory name is "foo“ , then vite devsever response vite_url:3000/foo , will throw this error.

@poyoho
Copy link
Member

poyoho commented Apr 28, 2022

https://stackblitz.com/edit/github-12zodn-j9tw2b?file=server.js I test it, it maybe is not the vite bug?

@johnnyasantoss
Copy link

We fixed the issue using the workaround provided by #6859 (comment).

Adding "main" to our package.json fixed the issue. This would still be a bug if adding it shouldn't be necessary. Otherwise, is fixed.

@poyoho
Copy link
Member

poyoho commented Apr 29, 2022

oh sorry, I think it same with vitejs/vite-plugin-vue#25 ?

@saurabhdaware
Copy link
Contributor

Not sure if I understood the 7220 but it seems a bit different to me.

For me this is happening when I pass <style></style> to vite.transformIndexHtml

I added some logs in this - https://stackblitz.com/edit/github-12zodn-joz2gx?file=server.js&terminal=dev

You can see that it is failing on transformIndexHtml 🤔

@chris-sab
Copy link

chris-sab commented Aug 17, 2022

Hello @poyoho Nice to meet you. Now I am having this issue:

[vite] Internal server error: Cannot set properties of undefined (setting 'isSelfAccepting')
  Plugin: vite:import-analysis

When I pnpm i, then it shows the above issue.
Vite version is 3.0.8 and Svelte version is 3.49.0 so how can fix this issue?

@poyoho
Copy link
Member

poyoho commented Aug 20, 2022

@chris-sab do you create the project start with root dir /?

@krisbitney
Copy link

I get a different, but possibly related bug when I try to build with Vite (first time user). The difference is that Vite is failing to link a dependency of one of my dependencies. I'm using Vite 3.0.9 on MacOS.

[commonjs] Failed to resolve entry for package "@opentelemetry/otlp-exporter-base". The package may have incorrect main/module/exports specified in its package.json.
file: /Users/kris/WebstormProjects/polywrap/demos/hello-world/app/solidjs/node_modules/@polywrap/core-js/build/uri-resolution/resolvers/index.js
error during build:
Error: Failed to resolve entry for package "@opentelemetry/otlp-exporter-base". The package may have incorrect main/module/exports specified in its package.json.
    at packageEntryFailure (file:///Users/kris/WebstormProjects/polywrap/demos/hello-world/app/solidjs/node_modules/vite/dist/node/chunks/dep-0fc8e132.js:34705:11)
    at resolvePackageEntry (file:///Users/kris/WebstormProjects/polywrap/demos/hello-world/app/solidjs/node_modules/vite/dist/node/chunks/dep-0fc8e132.js:34702:5)
    at tryNodeResolve (file:///Users/kris/WebstormProjects/polywrap/demos/hello-world/app/solidjs/node_modules/vite/dist/node/chunks/dep-0fc8e132.js:34452:20)
    at Object.resolveId (file:///Users/kris/WebstormProjects/polywrap/demos/hello-world/app/solidjs/node_modules/vite/dist/node/chunks/dep-0fc8e132.js:34254:28)
    at file:///Users/kris/WebstormProjects/polywrap/demos/hello-world/app/solidjs/node_modules/rollup/dist/es/shared/rollup.js:22695:37
error Command failed with exit code 1.

@xpluscal
Copy link

@krisbitney did you end up figuring this out?

@ghyath5
Copy link

ghyath5 commented Jan 7, 2023

Still having same issue !!!

@levitomer
Copy link

Same issue for me as well

node: 18.12.1
pnpm: 7.20.0
vite: 2.9.10
vue: 3.2.45

✘ [ERROR] [plugin vite:dep-scan] Failed to resolve entry for package "primevue/menuitem". The package may have incorrect main/module/exports specified in its package.json: Failed to resolve entry for package "primevue/menuitem". The package may have incorrect main/module/exports specified in its package.json.

@emahuni
Copy link

emahuni commented Jan 24, 2023

I have also noticed that if vite can't find the lib in question it reports using this error. Instead of saying it's missing it says something else which is confusing. I have solved this by adding the package to the lib that was missing.

@Chambo015
Copy link

Chambo015 commented Feb 24, 2023

I had the same error as you and the reason was in <video poster="/">
because the poster had the path '/' stupid mistake)

@dnasir
Copy link

dnasir commented Mar 3, 2023

I started having this issue when one of my dependencies switched from using the main field to browser field for its main entry point.

The only solution I have found is to declare an alias for this dependency pointing to its entry point in the vite.config.ts file.

// simplified for brevity
export default defineConfig({
  resolve: {
    alias: {
      'leaflet.gridlayer.googlemutant': './node_modules/leaflet.gridlayer.googlemutant/dist/Leaflet.GoogleMutant.js'
    }
  }
})

I hope this gets fixed soon.

@gocoffeecup
Copy link

I've the same issue, I cloned https://github.com/vitejs/vite-plugin-vue and tried the /playground/ssr-vue example, after installing with pnpm and npm run dev

> @vitejs/test-ssr-vue@0.0.0 dev
> node server

✘ [ERROR] [plugin externalize-deps] Failed to resolve entry for package "@vitejs/plugin-vue". The package may have incorrect main/module/exports specified in its package.json.

Node.js v19.7.0

@ATypescriptEnjoyer
Copy link

ATypescriptEnjoyer commented Mar 17, 2023

This is replicable using React with React Router too. For anyone else with the same problem, as the above says just set your main in package.json to:

"main": "./src/main.tsx",

@Damilare1
Copy link

I had the same error, deleting node_modules and running npm install again worked for me with Vite version on v4.1.4

@Niphram
Copy link

Niphram commented Jun 9, 2023

Just encountered something that may be very much related to this in SvelteKit (Although it also happens when using vite directly)
When trying to access localhost:<port>/some/path if the directory /some/path on your disk happens to contain a package.json (Doesn't matter what directory) then you get the error Failed to resolve entry for package "/home/niphram/Documents". The package may have incorrect main/module/exports specified in its package.json. (In this case I just put a package.json in my documents folder and tried to access localhost:5173/home/niphram/Documents)

It doesn't matter which directory it is, could be totally unrelated to the directory vite was started in. This probably shouldn't happen.

@claude-at-bit
Copy link

have you resolved this? i had the same issue with primevue/menuitem

@justinrlle
Copy link

justinrlle commented Sep 14, 2023

Hey, I've had this issue with primevue/menuitem and some other exports. The thing with the primevue/menuitem package is that it contains only types. If you do import { MenuItem } from 'primevue/menuitem', vite will try to import code from the package and fail to do so.

This is also cause by the way imports works in typescript, and more precisely import of types. Theses three following imports tell typescript to do different things, and only the last one is the good one in our case:

// try to import the symbol MenuItem of the package, fails here because there is no code
import { MenuItem } from 'primevue/menuitem';
// translated to the following javascript
import { MenuItem } from 'primevue/menuitem';

// does not try to import the MenuItem symbol, but still try to import the package, in case the package does some side effects
// and so fails, because there is still no code
import { type MenuItem } from 'primevue/menuitem';
// translated to the following javascript
import {} from 'primevue/menuitem';

// does not try to import the MenuItem symbol nor the package
import { type MenuItem } from 'primevue/menuitem';
// translated to no javascript at all, the whole import is eluded, and vite is happy

I had the error personally because we used the typescript-eslint rule consistent-type-imports with the fix style as inline-type-imports, so it introduced automatically the wrong kind of type import. Changing this imports to the last kind fixed the issue for us.

@bluwy bluwy added p3-minor-bug An edge case that only affects very specific usage (priority) and removed pending triage labels Sep 22, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Nov 7, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p3-minor-bug An edge case that only affects very specific usage (priority)
Projects
None yet
Development

Successfully merging a pull request may close this issue.