-
Notifications
You must be signed in to change notification settings - Fork 495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ReferenceError: window is not defined #121
Comments
i haven't tested ssr. i'll take a look as soon as i get a chance. |
@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 I know the issue is coming from a dependency, because 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? |
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 |
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: |
This is due to style loader. module of webpack. webpack-contrib/style-loader#272 (comment). I guess we have to use different style loader if you want to support server side rendering. Do you support server side rendering ? |
I am seeing this as well, any leads anyone? |
@Alchemist-V are you using server side rendering? if yes can you disable it if not then you should use different style loader. |
@indolent-developer have you got this working with SSR? If so could you point me to a working fork/pull req? |
@Rid I was able to work this via disabling server side rendering of this component. No fork or pull. |
@indolent-developer I got it working by trying to load the component lazily. Something like this:
|
@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 :( |
@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 |
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. |
i can try in few weeks currently tied up. |
Thanks @Rid ! Let me see if i can file a PR for this. Hope the above hack works for all in the mean time. |
Is there any updates on this ? |
I'm also interested in this. |
Same^ |
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 |
Checkout: |
Same error. |
Same error here. |
I am using it with Gatsby, and get the same error. |
one note: the hack code does not work for my case. |
My workaround has two parts:
<div>
{ typeof window !== 'undefined' && ReactJsonViewt && <ReactJsonView />}
</div>
// 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(),
},
],
},
});
} |
import dynamic from "next/dynamic"; const DynamicReactJson = dynamic(import('react-json-view'), { ssr: false }); This solved the issue. |
Confirming this issue while production building in Docusaurus v2. Not recommending with Docusaurus. |
@arifszn I'm the current lead maintainer of Docusaurus and you should just do something similar to what I did here: #121 (comment) I wouldn't say this lib is not recommended for Docusaurus, but as any lib without proper SSR support, we have to work around the lack of SSR support. |
@slorber I tried using react-json-inspector as an alternative but same error occurred. Then switched to react-json-tree and it is running perfectly without any hack. I would recommend react-json-tree for Docusaurus. Running Example is here: https://arifszn.github.io/reddit-image-fetcher/docs/#example |
This is not really a "hack", it's just rendering it only on the client, which is a technique you should have in your bag as a React developer. I mean, you shouldn't dismiss a lib just for lack of SSR support, particularly for things like json trees for which SEO does not matter much. Here's also a working version of this lib in Docusaurus: https://v2.docusaurus.io/__docusaurus/debug/ I agree that react-json-tree looks nice and is probably easier to integrate with Docusaurus (or Gatsby or NextJS), but that should not be the only thing to consider when making a choice. |
@thomasjm and @justinmchase, first of all, thank you for your amazing work! |
@correiamp I think it just needs some review. If people could try it out and chime in that it works and doesn't mess anything up visually, hopefully we can get it merged. (Last I checked this project was only semi-maintained.) You can stick the following in your "react-json-view": "git+https://github.com/thomasjm/react-json-view.git#e9baddc761536929f436e93baa5cebc5be519417" |
@correiamp I'm not a developer on this project but I would recommend everyone go to the PR (#298) and vote there because the developers appear to be simply unaware of the PR completely. If they were to see enough votes they may be inclined to merge it. |
@thomasjm and @justinmchase sorry the late reply. Going to add this feedback to the PR |
@thomasjm Im using your package version on SSR side. But when using it as the simples demo example showcase, I get an empty object back, and it fails my render. I wonder why as I cannot find anything in the docs pointing to renderJson should return an empty object. |
@vongohren I'm not sure, but there's been a bunch of commits on |
Nothing helped, so skipping this lib |
I'm also getting a similar error, it works right after restarting the develop local server but any time after it fails with this message:
I'm using this with Gatsby.js |
I got around using this: export const BrowserOnlyReactJson = (props) => {
if (typeof window === 'undefined') {
return null;
}
const ReactJson = require('react-json-view').default;
return <ReactJson {...props} />;
}; |
If you're using Next.js, you can use Dynamic Imports: import dynamic from 'next/dynamic';
const BrowserReactJsonView = dynamic(() => import('react-json-view'), {
ssr: false,
});
function Page() {
const obj = {
obj: 'example'
}
return (
<div>
<BrowserReactJsonView src={obj} />
</div>
);
} |
This worked for me using Gatsby.js, thank you @marlonguimaraes ! |
Problem was with SSR during gatsby build Solution was to check if it is browser before importing and rendering react-json-view Refs: mac-s-g/react-json-view#121 (comment) https://stackoverflow.com/a/32598826
This can cause hydration issues because the JSX rendered on the server/client is not exactly the same. React 18 will likely emit an hydration error BTW. |
Thanks for the hot tip @himself65! Works perfect with Next.js SSR. |
I like to wrap it in this component: import { useEffect, useState } from "react";
interface Props {
fallback?: JSX.Element; // JSX.Element
}
export const NoSSR = ({
children,
fallback,
}: React.PropsWithChildren<Props>): JSX.Element => {
const [mounted, setMounted] = useState(false);
useEffect(() => setMounted(true), []);
if (!mounted) {
return fallback || <></>;
}
return children as JSX.Element;
}; |
Next.js 14.2.15 with React 18 'use client'
import dynamic from 'next/dynamic'
const ReactJson = dynamic(() => import('react-json-view'), { ssr: false })
export function HighlightedJSON({ data }: { data: object }) {
return (
<ReactJson
src={data}
theme="bright:inverted"
collapsed={false}
displayObjectSize={false}
enableClipboard={false}
displayDataTypes={false}
groupArraysAfterLength={5}
style={{
padding: 10,
}}
/>
)
} |
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 !!!
The text was updated successfully, but these errors were encountered: