scniro / react-codemirror2

Codemirror integrated components for React
MIT License
1.65k stars 192 forks source link

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

Open frackjs opened 3 years ago

frackjs commented 3 years ago

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.

VictorKolb commented 3 years ago

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

yiliansource commented 3 years ago

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.
}