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

ReferenceError: navigator is not defined

Open mattmacpherson opened this issue 7 years ago • 14 comments

ReferenceError: navigator is not defined W20161022-17:29:38.375(-4)? (STDERR) at /Users/mattmacpherson/Code/project/node_modules/codemirror/lib/codemirror.js:24:19 W20161022-17:29:38.375(-4)? (STDERR) at userAgent (/Users/mattmacpherson/Code/project/node_modules/codemirror/lib/codemirror.js:12:22) W20161022-17:29:38.376(-4)? (STDERR) at Object.<anonymous> (/Users/mattmacpherson/Code/project/node_modules/codemirror/lib/codemirror.js:17:3) W20161022-17:29:38.376(-4)? (STDERR) at Module._compile (module.js:409:26) W20161022-17:29:38.376(-4)? (STDERR) at Object.Module._extensions..js (module.js:416:10) W20161022-17:29:38.376(-4)? (STDERR) at Module.load (module.js:343:32) W20161022-17:29:38.377(-4)? (STDERR) at Module.Mp.load (/Users/mattmacpherson/.meteor/packages/babel-compiler/.6.9.1_1.15j1r1l++os+web.browser+web.cordova/npm/node_modules/reify/node/runtime.js:16:23) W20161022-17:29:38.377(-4)? (STDERR) at Function.Module._load (module.js:300:12) W20161022-17:29:38.378(-4)? (STDERR) at Module.require (module.js:353:17) W20161022-17:29:38.378(-4)? (STDERR) at require (internal/module.js:12:17)

I only get this error when requiring modes:

require('codemirror/mode/javascript/javascript'); require('codemirror/mode/xml/xml'); require('codemirror/mode/markdown/markdown');

mattmacpherson avatar Oct 22 '16 21:10 mattmacpherson

Are you using server side rendered react? There won't be a navigator until the doc is ready.

wrannaman avatar Jan 16 '17 01:01 wrannaman

This was supposedly fixed but still happens as noted in #21

I would also like to use this package with SSR and currently cannot for this reason.

sslotsky avatar Feb 01 '17 22:02 sslotsky

Seems it was removed here:

https://github.com/JedWatson/react-codemirror/commit/4624b3d067d566ea3e8a1c73b79300d6babde956#diff-2c23fe372e330148850d3aa80cedc390

sslotsky avatar Feb 01 '17 22:02 sslotsky

@Method-X @sslotsky had the same issue here using next.js and SSR - I've worked around this issue by importing as follows:

let CodeMirror = null;
if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') {
  CodeMirror = require('react-codemirror');
  require('codemirror/mode/yaml/yaml');
  require('codemirror/mode/dockerfile/dockerfile');
}

And then rendering:

// render
   { CodeMirror && <CodeMirror ... /> }

After which it's only the client now that renders, on the server it's ignored. Does the job 👍

Salakar avatar Feb 13 '17 17:02 Salakar

@Salakar Thanks that worked fantastically!

For what it's worth, require('react-codemirror') itself does not produce the error. Just including the modes does. So you could make that first require unconditional and render unconditionally as well.

sslotsky avatar Feb 20 '17 04:02 sslotsky

I have a similar error, when i try to use react-player dependency, someone could helpme ReferenceError: navigator is not defined at FilePlayer.shouldUseHLS (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-player\lib\players\FilePlayer.js:173:41 ) at FilePlayer.render (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-player\lib\players\FilePlayer.js:268:25) at resolve (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-dom\cjs\react-dom-server.node.development.js:2149:18) at ReactDOMServerRenderer.render (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-dom\cjs\react-dom-server.node.dev elopment.js:2260:22) at ReactDOMServerRenderer.read (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-dom\cjs\react-dom-server.node.devel opment.js:2234:19) at renderToString (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\react-dom\cjs\react-dom-server.node.development.js:250 1:25) at renderPage (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\next\dist\server\render.js:174:26) at Function.getInitialProps (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\next\dist\server\document.js:83:25) at _callee$ (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\next\dist\lib\utils.js:36:30) at tryCatch (C:\Users\Pavilion\Documents\Proyectos\WebAppRioDanta\node_modules\regenerator-runtime\runtime.js:62:40

ErwinSalas avatar Jan 24 '18 04:01 ErwinSalas

It seems the trick from above no longer works in NextJs. This is a terrible hack but the following works for now (if there is a better way in nextjs to do this please share).

In the imports/requires

let CodeMirror = null
try {
  navigator
  CodeMirror = require('react-codemirror')
  require('codemirror/mode/htmlmixed/htmlmixed')
  require('codemirror/mode/handlebars/handlebars')
} catch (error) {}

and in the render method

// render
   { CodeMirror && <CodeMirror ... /> }

jflewis avatar Sep 06 '18 19:09 jflewis

thank you :)

x5engine avatar Jan 14 '19 01:01 x5engine

if you are using NextJS, maybe you can use this easier way

import dynamic from 'next/dynamic'; const CodeMirror = dynamic(() => import('react-codemirror'), { ssr: false });

songyule avatar Mar 19 '20 06:03 songyule

@songyule Can you please help me with the full code where it is used please ?

prashantchothani avatar Jun 01 '20 05:06 prashantchothani

Thanks @Salakar and @sslotsky . works for me as well

rohitninawe avatar Jun 01 '20 06:06 rohitninawe

if you are using NextJS, maybe you can use this easier way

import dynamic from 'next/dynamic'; const CodeMirror = dynamic(() => import('react-codemirror'), { ssr: false });

Something like this should work, @prashantchothani

import React, { useState } from 'react'
import dynamic from 'next/dynamic'

import 'codemirror/lib/codemirror.css'

const CodeMirror = dynamic(() => {
  import('codemirror/mode/javascript/javascript')
  return import('react-codemirror')
}, { ssr: false })

export const CodeEditor = () => {
  const [code, setCode] = useState(null)

  const options = { lineNumbers: true, mode: 'javascript' }

  return CodeMirror && <CodeMirror
    onChange={code => setCode(code)}
    options={options}
    value={code}
  />
}

export default CodeEditor

sombreroEnPuntas avatar Jun 17 '20 13:06 sombreroEnPuntas

Thanks @sombreroEnPuntas It worked

prashantchothani avatar Jun 18 '20 05:06 prashantchothani

react-codemirror2 this method working.

import { Controlled as CodeMirror } from "react-codemirror2";

let modeLoaded = false if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') { require('codemirror/mode/javascript/javascript') modeLoaded = true } const Editor = (props) => { const options = { autoCloseBrackets: true, lineWrapping: true, mode: language, } if (modeLoaded) options.mode = 'javascript'

return ( <CodeMirror onBeforeChange={handleChange} onKeyDown = {handleKeyup} value={value} className={"demo"} options={options} /> ) }

saravanan-trika avatar Dec 08 '21 06:12 saravanan-trika