5

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 ?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
kiran
  • 1,246
  • 1
  • 10
  • 16

2 Answers2

1

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.

daniula
  • 6,898
  • 4
  • 32
  • 49
0

Well this is quite old question still if some one is looking at it.

Try

if (typeof window === 'undefined') {
  global.window = {};
}
Ankit Balyan
  • 1,319
  • 19
  • 31