Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle window object on nodejs for server-side rendering of reactjs application

I am trying to implement server-side rendering for my reactjs application. I am using webpack to build reactjs application, enhanced-resolve to handle importing of jsx files in nodejs.

My application depends on third party libraries like enquire.js. When react application tries to import enquire.js on nodejs, it fails with error ReferenceError: window is not defined

Since window object is not available nodejs how to handle libraries that use window for server side rendering ?

like image 530
kiran Avatar asked Nov 09 '22 19:11

kiran


1 Answers

I haven't used Webpack yet. However, I had similar issue using browserify and I solved it by creating two builds: one for browser one for node.js with list of modules to ignore. You could achieve the same effect by adding to webpack config:

{
    plugins: [
        new webpack.IgnorePlugin(/file\.js$/)
    ]
}

Then you have to make sure that you are not making any calls to enquire.js by checking if require() returned false value or object with module functions.

like image 73
daniula Avatar answered Nov 14 '22 21:11

daniula