react-json-view icon indicating copy to clipboard operation
react-json-view copied to clipboard

ReferenceError: window is not defined

Open hnhatr opened this issue 7 years ago • 58 comments

From the latest version, server-side rendering produces following issue

ReferenceError: window is not defined at C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:150552 at C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:150507 at e.exports (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:151009) at Object. (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:146201) at t (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:377) at Object.defineProperty.value (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:41330) at t (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:377) at Object.defineProperty.value (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:39991) at t (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:377) at t.exports (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:734) at C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:744 at n (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:81) at Object. (C:\Users\hnhatr\Downloads\reactGo-master\node_modules\react-json-view\dist\main.js:1:253) at Module._compile (module.js:569:30) at Object.Module._extensions..js (module.js:580:10) at Module.load (module.js:503:32) at tryModuleLoad (module.js:466:12) at Function.Module._load (module.js:458:3) at Module.require (module.js:513:17) at require (internal/module.js:11:18) at Object. (C:\Users\hnhatr\Downloads\reactGo-master\compiled\webpack:\external "react-json-view":1:1) at webpack_require (C:\Users\hnhatr\Downloads\reactGo-master\compiled\webpack:\webpack\bootstrap deda87cb735796675547:19:1)

help me please !!!

hnhatr avatar Sep 28 '17 14:09 hnhatr

i haven't tested ssr. i'll take a look as soon as i get a chance.

mac-s-g avatar Sep 29 '17 17:09 mac-s-g

@hnhatr i'm having trouble reproducing the error. The error output you posted comes from the uglified and minified build, so it's tough to see where window is being referenced.

I know the issue is coming from a dependency, because window is not referenced in my src/ directory. I could try to track down what dependency is causing the issue.

can you tell me more about what tools & config you're using for server-side rendering?

is there a github link that will give me some more info about your build?

mac-s-g avatar Oct 01 '17 22:10 mac-s-g

also, @hnhatr , it looks like you're not the only one running into "window is not defined" issues when using server-side rendering.

a google search shows quite a bit of people running into the same issue.

there should be tools to emulate the window global on the server. here is an example of someone solving the problem by faking a window variable. I also believe there are plugins for ssr that initialize a global window object.

mac-s-g avatar Oct 01 '17 22:10 mac-s-g

I'm having the same issue, none of the hacks seem to work as the plugin is loaded using webpack before the global can be set.

I believe the included lib with is causing the issue is: https://github.com/zenorocha/clipboard.js/blob/a55c9ac513da6e6d01dfd6ceed9a930c596cf7e4/src/clipboard-action.js#L141

Rid avatar Oct 19 '17 17:10 Rid

This is due to style loader. module of webpack. https://github.com/webpack-contrib/style-loader/issues/272#issuecomment-345274784.

I guess we have to use different style loader if you want to support server side rendering. Do you support server side rendering ?

indolent-developer avatar Nov 18 '17 09:11 indolent-developer

I am seeing this as well, any leads anyone?

rajoriav avatar Dec 12 '17 10:12 rajoriav

@Alchemist-V are you using server side rendering? if yes can you disable it if not then you should use different style loader. something similar to https://github.com/kriasoft/isomorphic-style-loader

indolent-developer avatar Dec 12 '17 13:12 indolent-developer

@indolent-developer have you got this working with SSR? If so could you point me to a working fork/pull req?

Rid avatar Dec 15 '17 09:12 Rid

@Rid I was able to work this via disabling server side rendering of this component. No fork or pull.

indolent-developer avatar Dec 16 '17 12:12 indolent-developer

@indolent-developer I got it working by trying to load the component lazily. Something like this:

var ReactJson = null; class JsonEditor extends React.Component{ constructor() { super(); } componentDidMount() { require.ensure(['react-json-view'], function() { try { ReactJson = require('react-json-view').default; } catch(err) { console.log('react-json-view:', err); } }) } render () { return ( { ReactJson == null ? "Unable to load content": <ReactJson src={context} onEdit={this.onEdit.bind(this)} /> } ) } Any concerns or feedback?

rajoriav avatar Dec 18 '17 07:12 rajoriav

@Alchemist-V looks fine to be.

We do need a longterm solution for this though. A PR from some one. What does author have to say ? I am currently tied up :(

indolent-developer avatar Dec 18 '17 07:12 indolent-developer

@Alchemist-V I can confirm it's working, thanks! I think a fork/pull req using isomorphic-style-loader is better but I don't have time to test right now

Rid avatar Dec 20 '17 09:12 Rid

I'm totally cool with swapping out the loaders if that fixes the issue. anyone want to post a PR? I'm happy to test, merge, and publish.

mac-s-g avatar Dec 20 '17 21:12 mac-s-g

i can try in few weeks currently tied up.

indolent-developer avatar Dec 21 '17 05:12 indolent-developer

Thanks @Rid ! Let me see if i can file a PR for this. Hope the above hack works for all in the mean time.

rajoriav avatar Dec 21 '17 17:12 rajoriav

Is there any updates on this ?

mansdahlstrom1 avatar May 30 '18 12:05 mansdahlstrom1

I'm also interested in this.

charlieprynn avatar Jun 05 '18 14:06 charlieprynn

Same^

janouser11 avatar Jun 12 '18 14:06 janouser11

Last week I had this working perfectly fine, after restarting project I only get a window is not defined error. Any suggestions?

Thanks

P.S. The hack above worked, but not ideal

janouser11 avatar Jun 12 '18 14:06 janouser11

Same error.

xuchen81 avatar Oct 18 '18 01:10 xuchen81

Same error here.

tomkiss avatar Nov 05 '18 14:11 tomkiss

I am using it with Gatsby, and get the same error.

johnking avatar Nov 07 '18 04:11 johnking

one note: the hack code does not work for my case.

johnking avatar Nov 07 '18 04:11 johnking

My workaround has two parts:

  1. use conditional rendering in your component like the code snippet below
<div>
  { typeof window !== 'undefined' && ReactJsonViewt &&  <ReactJsonView  />}
</div>
  1. Modify your webpack config to avoid loading the react-json-view during stage build-html, something like below:
  // workaround for using react-json-view
  if (stage === 'build-html') {
    // console.log('inside build-html ...');
    actions.setWebpackConfig({
      module: {
        rules: [
          {
            test: /react-json-view/,
            use: loaders.null(),
          },
        ],
      },
    });
  }

johnking avatar Nov 07 '18 20:11 johnking

For users using nextjs, you can use next/dynamic for this case const DynamicReactJson = dynamic(import('react-json-view'), { ssr: false });

return your json view with data like <DynamicReactJson src={yourData}

hoangdoan267 avatar Nov 09 '18 06:11 hoangdoan267

I solved this with the following code:

render() {
        let JsonDom;
        const { info } = this.state;
        if (!canUseWindow || !info) {
            JsonDom = <div />;
        } else {
            var ReactJson = require('react-json-view');
            JsonDom = <ReactJson.default src={info} />;
        }
        return (
            <div>
                <Button type="primary" block onClick={this.getDeviceInfo}>
                    getDeviceInfo
                </Button>
                <div>{JsonDom}</div>
            </div>
        );
    }

If you have a better way, please call me Thanks

MinosIE avatar Jan 24 '19 06:01 MinosIE

Same error.

The minified code at the point of the error is reported has the following code:

(function(){return window&&document&&document.all&&!window.atob})

Its failing at that first reference to window, and is very near some other code related to base64 encoding data urls. This error appears shortly after ""The style-loader cannot be used in a non-browser environment".

I've confirmed its this line: https://github.com/mac-s-g/react-json-view/blob/master/webpack/webpack.config.js#L55

I've cloned and linked this repo locally and I can confirm that removing that loader from the webpack config allows the SSR to complete successfully (not sure if the component stops working or not though).

Here is a bug in that library that is related to this issue: https://github.com/webpack-contrib/style-loader/issues/272

Here is another bug which appears to fix this specific issue: https://github.com/webpack-contrib/style-loader/pull/250

There are multiple PR's in that repo that have been closed as won't fix for this issue because the project owner seems to think that this library wont' work in SSR environments anyway and so it shouldn't be fixed.

Here is the current code, which is different than the version we are using: https://github.com/webpack-contrib/style-loader/blob/de38c395cdf59f2babe54bf0e7e2f816baf36c74/src/runtime/injectStylesIntoStyleTag.js#L11

Updating style-loader to the latest version fixes this error but then it throws a new error later on saying document is not defined in a different spot...

I hate to say it but I think the real solution here is to redo react-json-view's styling entirely to not be using sass or webpack to load and pack styles 😬 Or at the very worst, provide the css files separately and require the caller to figure out how they want to load the css files. Don't package the deployed code with built in css loading.

This is the line that probably needs to be removed ultimately: https://github.com/mac-s-g/react-json-view/blob/master/src/js/index.js#L13

And replace it with inline react styling: https://reactjs.org/docs/dom-elements.html#style

Until that happens, I think you have to render your component dynamically with SSR off, in my case like this:

import React from 'react'
import dynamic from 'next/dynamic'

const App = dynamic({
  loader: import ('../components/app'),
  ssr: false
})
export default Index () {
  return (<App />)
}

justinmchase avatar Dec 28 '19 04:12 justinmchase

Here's a little workaround inspired by the previous answers, while SSR is still not supported:

import React from 'react'

export default props => {
  if (typeof window !== "undefined") {
   const ReactJson = require('react-json-view').default
   return <ReactJson {...props} />
  }
  return null
}

Tested on Gatsby v2. No extra webpack config needed.

alexandre-lelain avatar Jan 17 '20 18:01 alexandre-lelain