-
Notifications
You must be signed in to change notification settings - Fork 321
"window is not defined" error when starting production #37
Comments
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 |
thanks! I will give that a try. Also I figured out that this only happens when using the |
Don't use libraries that depend on window in your components. |
@sokra How do you exclude a library from prerendering? Update So, instead of:
I did:
|
@Frikki thank you! had the same issue and this fix it. 👍 |
@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; |
because server can not find window, you can use this getInitialState() { |
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). |
Also, another trick you can use below! :-) I ran into the same error and saw some universal/isomorphic react/node boilerplates use this
|
@troutowicz thank you! |
Hy all |
In the
main.js
file built for production I am getting an error when runningnpm run start
I am using almost all default settings for my webpack configs. Any suggestions on where to look to find out why this is happening?
The text was updated successfully, but these errors were encountered: