jackspirou/clientjs

Server Error ReferenceError: navigator is not defined

eeeman1 opened this issue · 6 comments

Server Error

ReferenceError: navigator is not defined

I don't get your use case. This lib detects a browser. You're trying to run it on a nodejs server. If you're doing this, you must provide the globals that this lib relies upon. If you don't, it will fail, nodejs doesn't provide the typical browser environment.

You can try to load it via jsdom or domino but I'm not sure what you're trying to achieve by running this lib server-side.

I'm closing this issue, feel free to reopen if you have further questions.

@BendingBender I'm also experiencing this issue when tried to start a React project.

I have the clientJs in a React component library (package.json) published in NPM, then install this library in a React project.

Then I got this error when try to build and start the project

at Object.getBrowser (webpack://...///node_modules/clientjs/src/vendor/deployJava.js?:1028:21) at Object.writePluginTag (webpack://...///node_modules/clientjs/src/vendor/deployJava.js?:1224:24) at eval (webpack://...//node_modules/clientjs/src/vendor/deployJava.js?:1269:12) at Object.../../node_modules/clientjs/src/vendor/deployJava.js at __webpack_require__ at fn at eval at Object at __webpack_require__ at fn
**ReferenceError: navigator is not defined**

Could you please suggest how to avoid this issue?

You could use the base bundle which doesn't depend on Java detection, you can find it here: dist/client.base.min.js. But if you're not using jsdom or a similar lib in your backend then it probably will fail elsewhere. As I said, NodeJs is not a browser so if you try to use it as a browser it's your responsibility to make sure that browser APIs are available when you run libs that depend on them.

@BendingBender I have just tried the dist/client.base.min.js solution. I tried the following but would like to confirm if it is correct as I'm still getting "var browser = navigator.userAgent.toLowerCase(); ReferenceError: navigator is not defined" error

  1. npm install clientjs
  2. add <script src="client.base.min.js"></script> in the index.html
  3. import { ClientJS } from "clientjs"; in a React component
  4. Use it, const client = new ClientJS();

after installing this React library into another React project, I'm still getting the same error. and If step 3) is not imported, the new ClientJS() is undefined. "Uncaught ReferenceError: ClientJS is not defined"

You shoud import it like this (no need for a script tag if you have a bundler like Webpack):

import { ClientJS } from "clientjs/dist/client.base.min";
// or
import { ClientJS } from "clientjs/src/client.base";