gatsby: Window is not defined at production build
Description
I want to trigger window width to change the content of my website. But I don’t know how to fix my issue. Webpack error : window is not defined at production build
Expected result
Production build successful.
Actual result
import React, { Component } from 'react'
import PropTypes from 'prop-types'
import { Helmet } from 'react-helmet'
import Link from 'gatsby-link'
import 'flexboxgrid/dist/flexboxgrid.min.css';
import { Footer, Header } from '../components/organisms'
import { Modal, Project } from '../components/molecules'
import '../scss/main.scss'
class Index extends Component {
constructor(props) {
super(props)
this.state = {
width: window.innerWidth,
};
}
componentDidMount() {
const width = typeof window !== 'undefined' && window
}
componentWillMount() {
window.addEventListener('resize', this.handleWindowSizeChange);
}
// make sure to remove the listener
// when the component is not mounted anymore
componentWillUnmount() {
window.removeEventListener('resize', this.handleWindowSizeChange);
}
handleWindowSizeChange = () => {
this.setState({ width: window.innerWidth });
};
render(){
const { width } = this.state;
const isMobile = width <= 600;
console.log(width, ' px')
const {data} = this.props;
return(
<div className="wrapper">
<div className="wrapper__top">
<Helmet>
<html lang="fr" />
<meta charSet="utf-8" />
<meta httpEquiv="X-UA-Compatible" content="IE-edge,chrome=1" />
<meta name="viewport" content="width=device-width,initial-scale=1" />
<link rel="icon" type="image/png" href="/favicon.png" />
</Helmet>
<Header />
<main>
{/* Affichage du Teaser pour les navigateurs qui ont une largeur supérieure ou égale à 600px */}
{
(isMobile && this.props.location.pathname === '/') ?
<div>
{data.allProjectsJson.edges.map(({ node }, i) =>
(<Project
key={i}
title={node.title}
category={node.category}
image={{
src: node.image.childImageSharp.original.src,
alt: node.title,
}}
logo={{
src: node.logo.childImageSharp.original.src,
alt: node.title,
}}
website={node.website}
>
<p dangerouslySetInnerHTML={{ __html: node.description }} />
</Project>),
)}
</div>
: this.props.children()
}
</main>
<Footer />
</div>
</div >
)
}
}
Index.propTypes = {
children: PropTypes.func.isRequired,
location: PropTypes.shape({
pathname: PropTypes.string,
}).isRequired,
data: PropTypes.object.isRequired
};
export default Index;
export const getSiteHeader = (siteTitle, description) =>
(<Helmet>
<title>{`${siteTitle} | Maral Sabbagh`}</title>
<meta name="description" content={description} />
</Helmet>);
//eslint-disable-next-line no-undef
export const pageQuery = graphql `
query LayoutQuery {
allProjectsJson {
edges {
node {
title
category
description
image {
childImageSharp {
original {
src
}
}
}
logo {
childImageSharp {
original {
src
}
}
}
website
}
}
}
allGeneralJson(filter: {url: {eq: "/projets"}}){
edges{
node{
url
pageTitle
metaDesc
metaTitle
}
}
}
}
`;
Environment
gatsby info --clipboard
module.js:549
throw err;
^
Error: Cannot find module 'regenerator-runtime/runtime'
at Function.Module._resolveFilename (module.js:547:15)
at Function.Module._load (module.js:474:25)
at Module.require (module.js:596:17)
at require (internal/module.js:11:18)
at Object.<anonymous> (C:\Users\Maral Sabbagh\AppData\Roaming\npm\node_modules\gatsby-cli\lib\index.js:88:1)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
Thanks for your help
About this issue
- Original URL
- State: closed
- Created 6 years ago
- Reactions: 8
- Comments: 18 (5 by maintainers)
Posting this here since I spent a good chunk of my Friday on this last week.
window
can be fixed as mentioned in the docs. You can include multiplewindow
-dependent libraries in one go:export const isBrowser = () => typeof window !== 'undefined'
and then use it everywhere:import { isBrowser } from './utils'; {isBrowser() && <CodeThatNeedsWindow />
}I had this issue and I did this.
It solved my issue 😁
I strongly suggest you use react-media for this case.
But if you don’t want to, changing to this should fix it:
Also note that there will be small hit to performance, unless you debounce the resize eventListener. Let me know if you need more help!
@daxaxelrod i had a similar issue with that a while ago while trying to get a resize event to work. a good workaround that did the trick for me was:
I’m actually having a similar issue. I have this search page and I allow the user to press enter to fire off a redux action. When an action is dispatch, props changes forcing a rerender. When the user then goes to another page, i get window.removeEventListener is not defined. Here’s my code.
This isn’t issue with Gatsby. Just one of the gotchas of server-side rendering with React.
When Node tries to build, it instantiates the class, and in the constructor finds
window
, which is a browser global (Node doesn’t havewindow
).EDIT: it doesn’t have to be zero. Just as long as you don’t mention
window
it should be fine.(navigate('/app/login'))}
this navigate function of gatsby is causing issues for me. while running
npm build
it says:window is not available during server side rendering.
and :var navigate = function navigate(to, options) { window.___navigate(rewriteLinkPath(to, window.location.pathname), options); }; exports.navigate = navigate;
(indicates at
window
) Any possible solutions for me?@antoinerousseau wow I need to start using a linter. Thanks 😊
@daxaxelrod you have a typo, that’s why.
removeEventListner
=>removeEventListener
Anybody have a sense of how to fix this error for
AudioContext()
? The only way I’ve ever used it is mounting to thewindow
object - is there another way to do this? I’m trying to use thereact-media-player
component. Same issue as others in this thread - works fine in dev mode working with the client but when you build out for SSR, I’m getting thewindow is not defined
: