react-codemirror2 icon indicating copy to clipboard operation
react-codemirror2 copied to clipboard

importing mode file from the codemirror package gives you error in SSR

Open frackjs opened this issue 3 years ago • 2 comments

Although the react-codemirror2 package itself works fine in SSR (its import doesn't scream with error), the depended mode files can't be imported, such as codemirror/mode/javascript/javascript (it would give you an error that "navigator is not defined")

Although this mode file not part of this library, but it's part of the instructions in the README, and its a peer dependency.

It would be nice to provide workaround for importing the mode file in Next.js for instance.

frackjs avatar Jan 09 '21 02:01 frackjs

This was helped to me: https://github.com/ResourcesCo/snippets/blob/master/docs/codemirror-with-next.md

VictorKolb avatar Feb 08 '21 07:02 VictorKolb

Would be great if this could be mentioned in the README!

Edit: Here's the workaround I used (placed directly after the other imports):

if (typeof window !== "undefined") {
    require("codemirror/lib/codemirror.css");
    require("codemirror/mode/javascript/javascript");
    // ... other imports you might need.
}

yiliansource avatar May 22 '21 18:05 yiliansource