Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

BrowserDetector detects Node.js v21.0.0 and later as being a browser #4561

Closed
trentm opened this issue Mar 20, 2024 · 3 comments · Fixed by #4604
Closed

BrowserDetector detects Node.js v21.0.0 and later as being a browser #4561

trentm opened this issue Mar 20, 2024 · 3 comments · Fixed by #4604
Labels
bug Something isn't working pkg:resources priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect triage

Comments

@trentm
Copy link
Contributor

trentm commented Mar 20, 2024

What happened?

Steps to Reproduce

$ nvm use 21    # switch to Node.js v21 somehow

$ cd packages/opentelemetry-resources
$ npm test

Expected Result

Tests should pass.

Actual Result

They fail one test:

$ npm test
...
  Node.js: browserDetector()
    1) should return empty resources if window.document is missing
...
  56 passing (153ms)
  10 pending
  1 failing

  1) Node.js: browserDetector()
       should return empty resources if window.document is missing:

      AssertionError [ERR_ASSERTION]: Expected values to be strictly equal:

3 !== 0

      + expected - actual

      -3
      +0

      at assertEmptyResource (test/util/resource-assertions.ts:375:10)
      at Context.<anonymous> (test/detectors/node/BrowserDetector.test.ts:25:24)
      at processTicksAndRejections (node:internal/process/task_queues:95:5)

Additional Details

BrowserDetectorSync is using const isBrowser = typeof navigator !== 'undefined';

const isBrowser = typeof navigator !== 'undefined';

but Node.js v21.0.0 added a global navigator:
https://nodejs.org/api/all.html#all_globals_navigator

@trentm
Copy link
Contributor Author

trentm commented Mar 20, 2024

Thoughts on using window !== undefined?

Both latest Node.js and Bun have a navigator global now, FWIW.

@MSNev Any experience or opinions here for how to identify if we are in a browser runtime?

@MSNev
Copy link
Contributor

MSNev commented Mar 20, 2024

While window won't exist on node, so that would work. It also doesn't exist in a web worker either so that would still be a browser scenario.

I generally, go the other way around and detect whether the environment is node, see the "helpers" I have in the "Runtime Environment Checks" section of this https://github.com/nevware21/ts-utils?tab=readme-ov-file#documentation-and-details.

So basically, if something "wants" to use window it uses either getWindow() or hasWindow(), which are all wrapped in try / catch so that environments that don't like you trying to access globals that don't exist don't cause a failure.

And likewise for the getDocument() / hasDocument(), again workers generally don't have access to the DOM.

@pichlermarc pichlermarc added priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect pkg:resources labels Mar 27, 2024
@dyladan
Copy link
Member

dyladan commented Apr 3, 2024

I think testing node environment variables should be possible to detect if we're in node. Something like undefined != process?.env?.["NODE_VERSION"] maybe

trentm added a commit to trentm/opentelemetry-js that referenced this issue Apr 3, 2024
Zirak pushed a commit to Zirak/opentelemetry-js that referenced this issue Sep 14, 2024
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working pkg:resources priority:p2 Bugs and spec inconsistencies which cause telemetry to be incomplete or incorrect triage
Projects
None yet
4 participants