Importing CSS files in Isomorphic React Components

You can’t require css in the component that you are rendering on the server. One way to deal with it is to check if it’s a browser before requiring css.

if (process.env.BROWSER) {
  require("./style.css");
}

In order to make it possible you should set process.env.BROWSER to false (or delete it) on the server
server.js

delete process.env.BROWSER;
...
// other server stuff

and set it to true for the browser. You do it with webpack’s DefinePlugin in the config –
webpack.config.js

plugins: [
    ...
    new webpack.DefinePlugin({
        "process.env": {
            BROWSER: JSON.stringify(true)
        }
    })
]

You can see this in action in gpbl’s Isomorphic500 app.

Leave a Comment