ReferenceError: window is not defined
See original GitHub issueProblem
While I was following this tutorial to build a cloned code-pen apps using npm
instead of yarn, I received ReferenceError: window is not defined
I looked up some similar issue that was raised in the past, and people suggested to use brace
, but the latest react-ace
currently stops supporting brace
and suggesting us to use Ace-build which I did, but it fails with the same error.
node: v12.8.0 npm: 6.14.8
Sample code to reproduce your issue
import React from "react";
import AceEditor from "react-ace";
import "ace-builds/src-noconflict/mode-javascript";
import "ace-builds/src-noconflict/mode-html";
import "ace-builds/src-noconflict/mode-css";
import "ace-builds/src-noconflict/theme-monokai";
import styles from "./editors.module.css";
export const JavascriptEditor = () => {
return <Editor mode="javascript" title={"JS"} />;
};
export const HtmlEditor = () => {
return <Editor mode="html" title={"HTML"} />;
};
export const CssEditor = () => {
return <Editor mode="css" title={"CSS"} />;
};
const Editor = ({ mode, title }) => {
return (
<div className={styles.editorContainer}>
<div className={styles.editorTitle}>{title}</div>
{<AceEditor
mode={mode}
theme="monokai"
name={title}
fontSize={18}
width={"100%"}
showPrintMargin={true}
showGutter={true}
tabSize={2}
setOptions={{ useWorker: false }}
/>}
</div>
);
};
References
Progress on: #
Issue Analytics
- State:
- Created 2 years ago
- Comments:6
Top Results From Across the Web
How to solve "window is not defined" errors in React and Next.js
How to solve "window is not defined" errors in React and Next.js · 1. First solution: typeof · 2. Second solution: the useEffect...
Read more >How to solve Next.js window is not defined
ReferenceError: window is not defined is a pretty common error you may run into when using Next.js for the first time but don't...
Read more >referenceerror: window is not defined, how to solve
Here's how to fix the “referenceerror: window is not defined” error that you might have in Node.js or with a tool like Next.js....
Read more >How To Solve ReferenceError window is not defined in ...
Fixing a window is not defined error can be quite simple. In most cases, all you will need to do is wrap your...
Read more >[Solved] ReferenceError : window is not defined - ItsJavaScript
The ReferenceError : window is not defined error mainly occurs if you are using the window object in Node.js, React.js, Next.js.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Same issue came up, it was because I was using react-ace with next.js. By default next.js renders pages server-side, hence the
window
object isn’t available.The fix was to dynamically import (see docs here) the module containing the AceEditor:
If you’re in a React + MUI + Next.js setup like me, you could also avoid the SSR (server-side rendering) like so:
Page:
Component:
based on the example from @syvlabs