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

Updating from vite 4.1.4 to 4.2.0 broke my build - "Internal server error: window is not defined" #12483

Closed
6 of 7 tasks
Spenhouet opened this issue Mar 19, 2023 · 6 comments
Closed
6 of 7 tasks

Comments

@Spenhouet
Copy link

Spenhouet commented Mar 19, 2023

Describe the bug

My app is a Svelte app which is build as static page. After the update from 4.1.4 to 4.2.0 the build is broken.

The release notes of 4.2.0 do not seem to contain any migration guide. Which suggest to me that this update breaking anything is not expected?

Reproduction

https://github.com/bcked/bcked.com/tree/981359b70c2a64e965408ba9f16908812be10a7b

Steps to reproduce

  • Run `npm install``
  • Run npm run dev -> this should work
  • Exit
  • Run npm update --save vite
  • Run npm run dev -> this now fails with the below exception

System Info

System:
    OS: Linux 5.10 Ubuntu 20.04.5 LTS (Focal Fossa)
    CPU: (16) x64 12th Gen Intel(R) Core(TM) i5-12600K
    Memory: 13.39 GB / 15.50 GB
    Container: Yes
    Shell: 5.0.17 - /bin/bash
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    npm: 9.2.0 - ~/.nvm/versions/node/v16.15.0/bin/npm
  npmPackages:
    vite: ^4.2.0 => 4.2.0

Used Package Manager

npm

Logs

After only performing this update (not other package was updated), suddenly my build throws the following error.

Click to expand!
5:37:27 PM [vite-plugin-svelte] ssr compile in progress ...
5:37:28 PM [vite] Error when evaluating SSR module /src/routes/(app)/sections/intro.svelte: failed to import "3d-force-graph"

5:37:28 PM [vite] Error when evaluating SSR module /src/routes/(app)/+page.svelte:

Internal server error: window is not defined
      at file:///home/spe/backed/node_modules/three-forcegraph/dist/three-forcegraph.mjs:416:15
      at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
      at async Promise.all (index 0)
      at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
      at async nodeImport (file:///home/spe/backed/node_modules/vite/dist/node/chunks/dep-c167897e.js:54024:21)
      at async eval (/src/routes/(app)/sections/intro.svelte:15:31)
      at async instantiateModule (file:///home/spe/backed/node_modules/vite/dist/node/chunks/dep-c167897e.js:53981:9)
ReferenceError: window is not defined
    at file:///home/spe/backed/node_modules/three-forcegraph/dist/three-forcegraph.mjs:416:15
    at ModuleJob.run (node:internal/modules/esm/module_job:198:25)
    at async Promise.all (index 0)
    at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
    at async nodeImport (file:///home/spe/backed/node_modules/vite/dist/node/chunks/dep-c167897e.js:54024:21)
    at async eval (/src/routes/(app)/sections/intro.svelte:15:31)
    at async instantiateModule (file:///home/spe/backed/node_modules/vite/dist/node/chunks/dep-c167897e.js:53981:9)

Validations

@fi3ework
Copy link
Member

Related to #12324. The package three-forcegraph and three-render-objects aren't compatible with SSR. Considering move 3d-force-graph to client only.

@bluwy
Copy link
Member

bluwy commented Mar 20, 2023

Yeah. The error has always been there, but Vite was incorrectly suppressing it before, which is fixed in Vite 4.2. I'd suggest following fi3ework's solution since I don't think we want to fix this now.

@bluwy bluwy closed this as not planned Won't fix, can't repro, duplicate, stale Mar 20, 2023
@Spenhouet
Copy link
Author

Spenhouet commented Mar 20, 2023

Hi @fi3ework, the code is already in a client side only block but I think it's already failing on the import. I will take another look if imports can somehow moved to client side only code.

Edit:
Short Google search suggests client side only imports should be doable: https://stackoverflow.com/a/63502144

@Spenhouet
Copy link
Author

Spenhouet commented Mar 20, 2023

Yeah. The error has always been there, but Vite was incorrectly suppressing it before, which is fixed in Vite 4.2. I'd suggest following fi3ework's solution since I don't think we want to fix this now.

@bluwy Might be worth a note/warning in the release notes? This took me by surprise and much longer than I would have liked since there was no information to go by.

@bluwy
Copy link
Member

bluwy commented Mar 20, 2023

It is noted at

* fix: throw ssr import error directly (fix #12322) (#12324) ([21ffc6a](https://github.com/vitejs/vite/commit/21ffc6a)), closes [#12322](https://github.com/vitejs/vite/issues/12322) [#12324](https://github.com/vitejs/vite/issues/12324)

@Spenhouet
Copy link
Author

That's not sufficient imo. Could be more explicit. I'm probably not the only one this will effect.

@github-actions github-actions bot locked and limited conversation to collaborators Apr 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

3 participants