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.

ReferenceError: window is not defined when importing progressive-image.js

See original GitHub issue

I want to add https://github.com/craigbuckler/progressive-image.js to my vite-ssg app and do this by adding

import 'progressive-image.js/dist/progressive-image.js'
import 'progressive-image.js/dist/progressive-image.css'

to the main.ts. On Build for server… i get ReferenceError: window is not defined. I used vitesse as base and know it’s possible to create a module. But i cannot import the two resources in a isClient if clause. Is there any way to import these resources only on client generation only?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

6reactions
userquincommented, Jun 16, 2021

The problem here is that libraries that are not SSR/SSG aware should be loaded with dynamic import, for example, for progressive-image:

 if (typeof window !== 'undefined') {
  import('progressive-image.js/dist/progressive-image.js')
 }

or using onMounted / beforeMount hooks.

You can see an example on src/modules/pwa.ts module on vitesse template, where the virtual:pwa-register module (this guy will register the service worker) is loaded only if isClient is true.

0reactions
PatrikBirdcommented, Jun 6, 2021

I have the same issue when I build for production (ReferenceError: window is not defined) while playing around with p5.js sketches using vitesse. I’d like to exclude the component from SSG but wrapping the components in <client-only> doesn’t work for me.

Read more comments on GitHub >

github_iconTop Results From Across the Web

WebpackError: ReferenceError: window is not defined on Gatsby
When using third-party dependencies (such as your Bootstrap modal) your capability to access to window object disappears.
Read more >
referenceerror: window is not defined, how to solve
Here's how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >
How to solve "window is not defined" errors in React and Next.js
A different solution is to load your Scroll component using dynamic imports and the srr: false option. This way your component won't even...
Read more >
MiniCssExtractPlugin - webpack
This plugin extracts CSS into separate files. It creates a CSS file per JS file which contains CSS. It supports On-Demand-Loading of CSS...
Read more >
Untitled
!localStorage. js world, window is not defined, window is only available in 23 ... 2020 · Working with Local Storage in a Blazor...
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