node.js - ReferenceError: navigator is not defined
Asked Answered
F

5

13

I'm getting ReferenceError: navigator is not defined running the following code on node.js:

navigator.geolocation.getCurrentPosition((data) => {
  console.log(data);
});

I assume that the problem is that Navigator.geolocation is a Web API and doesn't work on node. Is there a way to mock the browser and use it on node.js?

Many thanks

Fading answered 25/2, 2020 at 16:32 Comment(2)
Does this answer your question? require(processing-js) throws Reference Error: Navigator not FoundBlubberhead
What are you trying to do? Get the location of the computer Node.js is running on? Write unit tests for code that is intended to run in a browser (but with tests that run via Node.js)? Something else?Aglet
V
5

Is there a way to mock the browser and use it on node.js?

One library that is useful for mocking Web APIs is browser-env.

import browserEnv from 'browser-env';

Then:

browserEnv(['navigator']);

This pollutes the global namespace, though, so be circumspect in its use.

Vienne answered 22/10, 2020 at 15:1 Comment(0)
B
5

global.navigator worked for me:

global.navigator.geolocation.getCurrentPosition((data) => {
  console.log(data);
});
Browband answered 5/7, 2022 at 11:8 Comment(0)
E
4

So I created this because analytics does not support node, so it requires conditional importing

import isNode from 'detect-node'
export async function analyticsLogEvents(event, params) {
  if (!isNode) {
    const firebase = await import('firebase/app')
    await import('firebase/analytics')
    firebase.default.analytics().logEvent(event, params)
  }
}
Eipper answered 20/11, 2020 at 2:1 Comment(2)
Fantastic. Thank you very much. The isNode condition solves a big problem for me re: Next.js initializing route logic in Node (and the same logic being run later by the browser). I was on this stuck for several hours. Really, thanks again.Underthrust
@JohnCamden no problem, it took me like 2 days to find what was happening in my project because of the complexity between node and libraries and the react rendering, I'm glad it helped!Eipper
V
1

Node.js is a server side technology, which means it can't access the user's geolocation directly.

What you should be asking is how to get geolocation using javascript on the client side, and then simply send that information to your server via Ajax.

More info here: http://www.w3schools.com/html/html5_geolocation.asp .

There are of course libraries that will ease the use of this.

Valkyrie answered 25/2, 2020 at 16:39 Comment(0)
C
-1

Use BrowserEnv. You should get more infomation at https://www.npmjs.com/package/browser-env. Hope can help you!

Cytokinesis answered 4/4, 2022 at 9:45 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Decapolis
While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From ReviewSwashbuckling

© 2022 - 2024 — McMap. All rights reserved.