question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ReferenceError: window is not defined

See original GitHub issue

Problem

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:open
  • Created 2 years ago
  • Comments:6

github_iconTop GitHub Comments

45reactions
syvlabscommented, Apr 12, 2021

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:

import dynamic from 'next/dynamic'

const DynamicComponentWithNoSSR = dynamic(
  () => import('../components/module_with_editor'),
  { ssr: false }
)

function Home() {
  return (
    <div>
      <Header />
      <DynamicComponentWithNoSSR />
      <p>HOME PAGE is here!</p>
    </div>
  )
}

export default Home
0reactions
tudormunteanucommented, Oct 13, 2022

If you’re in a React + MUI + Next.js setup like me, you could also avoid the SSR (server-side rendering) like so:

Page:

import dynamic from 'next/dynamic'
import React from "react";
import Box from '@mui/material/Box';

const Ace = dynamic(
  () => import("./components/editor"),
  { ssr: false }
)

const Editor = () => {
    return (
      <Box>
        <Ace />        
      </Box>
    );
}

export default Editor;

Component:

import React from "react";
import { render } from "react-dom";
import AceEditor from "react-ace";
import Box from '@mui/material/Box';

import "ace-builds/src-noconflict/mode-python";
import "ace-builds/src-noconflict/theme-dracula";
import "ace-builds/src-noconflict/ext-language_tools";

function onChange(newValue) {
  console.log("change", newValue);
}

const Editor = (props) => {
  return (
    <Box>
      <Box id="example"></Box>
      <AceEditor
        mode="python"
        theme="dracula"
        onChange={onChange}
        name="example"
        editorProps={{ $blockScrolling: true }}
      />
    </Box>
)
};

export default Editor;

based on the example from @syvlabs

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found