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.

WebpackError: ReferenceError: window is not defined

See original GitHub issue

Bug description

👋🏽Hello! I’ve included this library as part of a Gatsby & React project, however my build fails when I import this package.

Step 1 of the Usage section is a simple import mediumZoom from 'medium-zoom'. When I add this import to the top of my JS file my build fails with the following response:

4:36:32 PM:   WebpackError: ReferenceError: window is not defined
4:36:32 PM:   
4:36:32 PM:   - medium-zoom.esm.js:1 Module../node_modules/medium-zoom/dist/medium-zoom.esm.    js
4:36:32 PM:     [lib]/[medium-zoom]/dist/medium-zoom.esm.js:1:1800

It looks like this is a common error where there’s no window server-side but this package’s code is being run & flagged by Webpack. Is there any way around this?

Gatsby has the following page with some possible leads to a solution— Debugging HTML Builds

I’ve tried the part about Fixing Third-party Modules, but to no avail. After editing the Webpack-Config the build completes, however, when calling the function the console says mediumZoom is not a function as if the import is ignored altogether.


How to reproduce

A list of the steps to reproduce the bug.

  1. Import medium-zoom per the docs
  2. Add mediumZoom(selector, options) call to ComponentDidMount (after the image elements are rendered)
  3. Works beautifully in Dev
  4. Run gatsby build to generate a production build
  5. See error

Expected behavior

The build should complete to deploy!

Environment

  • Browser: All
  • medium-zoom version: Latest

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
francoischalifourcommented, Dec 22, 2018

I gave it a try @DrianHillman and my recommendation would be to use a dynamic import:

class Template extends React.Component {
  zoom = null

  componentDidMount() {
    import('medium-zoom').then(mediumZoom => {
      this.zoom = mediumZoom.default('.zoomable img')
    })
  }

  componentWillUnmount() {
    if (this.zoom) {
      this.zoom.detach()
    }
  }
}
0reactions
DrianHillmancommented, Dec 23, 2018

🎉 This is now working beautifully, François — thanks for helping troubleshoot! @francoischalifour

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gatsby - the window is not defined error, what and how to fix it?
The nodejs environment doesn't have the browser-globals like window , document , etc. Hence if the source code has a reference to them,...
Read more >
Window is not defined · Issue #309 · gatsbyjs/gatsby - GitHub
Works fine in development but if I try to gatsby build , I get Error: ReferenceError: window is not defined . I'm doing...
Read more >
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 in GatsbyJS
Debugging why your GatsbyJS (or NextJS) build is failing when you receive the message 'window is not defined'.
Read more >
Debugging HTML Builds | Gatsby
js. If this is your problem you should see an error above like “window is not defined”. To fix this, find the offending...
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