Skip to content
This repository has been archived by the owner on Jan 16, 2018. It is now read-only.

"window is not defined" error when starting production #37

Closed
FoxxMD opened this issue Feb 19, 2015 · 11 comments
Closed

"window is not defined" error when starting production #37

FoxxMD opened this issue Feb 19, 2015 · 11 comments

Comments

@FoxxMD
Copy link
Contributor

FoxxMD commented Feb 19, 2015

In the main.js file built for production I am getting an error when running npm run start

$ npm run start

> react-starter@0.0.0 start /Users/mduncan/FDC/Code/com.fdc.ui
> node lib/server-production


/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:3944
        })( jQuery, window , document );
                    ^
ReferenceError: window is not defined
    at Object.<anonymous> (/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:3944:14)
    at Object.<anonymous> (/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:21890:31)
    at __webpack_require__ (/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:21:30)
    at Object.<anonymous> (/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:1269:2)
    at __webpack_require__ (/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:21:30)
    at Object.module.exports.desc.applyUpdate (/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:150:51)
    at __webpack_require__ (/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:21:30)
    at Object.<anonymous> (/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:52:15)
    at __webpack_require__ (/Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:21:30)
    at /Users/mduncan/FDC/Code/com.fdc.ui/build/prerender/main.js:41:18

npm ERR! Darwin 14.1.0
npm ERR! argv "node" "/usr/local/bin/npm" "run" "start"
npm ERR! node v0.10.35
npm ERR! npm  v2.5.0
npm ERR! code ELIFECYCLE
npm ERR! react-starter@0.0.0 start: `node lib/server-production`
npm ERR! Exit status 8

I am using almost all default settings for my webpack configs. Any suggestions on where to look to find out why this is happening?

@barrystaes
Copy link
Contributor

It looks like you are using window somewhere (for jQuery ?) and this variable sometimes (notably serverside) sometimes does not exist.

Try this fix i did in my ReactPlayground: https://github.com/barrystaes/ReactPlayground/commit/111503b23daaecc944ca0ce8f6ffa6430cd4b509

@FoxxMD
Copy link
Contributor Author

FoxxMD commented Mar 5, 2015

thanks! I will give that a try. Also I figured out that this only happens when using the prerender build option so it's not critical as I'm not using that for production.

@sokra
Copy link
Member

sokra commented Apr 17, 2015

Don't use libraries that depend on window in your components.
or
Only use these libraries in componentDidMount and exclude them for prerendering (make sure your component don't render different in prerendering or it doesn't work).

@sokra sokra closed this as completed Apr 17, 2015
@Frikki
Copy link

Frikki commented Apr 23, 2015

@sokra How do you exclude a library from prerendering?

Update
I figured I shouldn’t import the library outside the class definition, but instead require it in the componentDidMount method or a called method thereof.

So, instead of:

...
import MyWindowDependentLibrary from 'path/to/library';
...
export default class MyClass extends React.Component {
    ...
    componentDidMount() { MyWindowDependentLibrary.doWork(); }
    ...
}

I did:

// removed import
...
export default class MyClass extends React.Component {
    ...
    componentDidMount() {
        const MyWindowDependentLibrary = require( 'path/to/library' );
        MyWindowDependentLibrary.doWork();
    }
    ...
}

@juancabrera
Copy link

@Frikki thank you! had the same issue and this fix it. 👍

@troutowicz
Copy link

@Frikki Dependency declarations should be kept at the top of files. A better solution for your case would be to do a conditional require at the top of your component.

const isBrowser = typeof window !== 'undefined';
const MyWindowDependentLibrary = isBrowser ? require( 'path/to/library') : undefined;

@17Damon
Copy link

17Damon commented Jun 25, 2016

because server can not find window, you can use this

getInitialState() {
return {windowWidth: (typeof window !== 'undefined')? window.innerWidth:undefined};
},

@derekdreery
Copy link

You can't use conditional requires in ES6 module syntax. For this I tend to write no-op implementations of anything that is client-specific (alternatively they could throw on the server, to point out that their use is an error).

@Andriy-Kulak
Copy link

Also, another trick you can use below! :-) I ran into the same error and saw some universal/isomorphic react/node boilerplates use this

   if (process.env.BROWSER) {
      devToolsExtension = window.devToolsExtension();
    }

@rihannarickeminem
Copy link

@troutowicz thank you!

@ghost
Copy link

ghost commented Sep 10, 2017

Hy all
I have this problem for server rendering.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants