react-codemirror2
react-codemirror2 copied to clipboard
importing mode file from the codemirror package gives you error in SSR
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.
This was helped to me: https://github.com/ResourcesCo/snippets/blob/master/docs/codemirror-with-next.md
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.
}