browserify / browserify/randombytes

`global` Reference Error in Browser Environment

Open
#38 0 comments 2 reactions 0 assignees View on GitHub
Dominant language
JavaScript
Stars
102
Forks
47
PR merge metrics
No merged PRs in 30d

Description

When using `randombytes@2.1.0` in a browser environment with Vite as the build tool, the following error occurs:

```bash
Uncaught ReferenceError: global is not defined
```

This error stems from the browser.js implementation attempting to access the `global` object which is not available in browser DOM environments:

```javascript
var Buffer = require('safe-buffer').Buffer
var crypto = global.crypto || global.msCrypto
```

As the source code mentioned below `crypto` is defined correctly

https://github.com/browserify/randombytes/blob/b4eb4126fccc503e375853ca7b3e96aaa34af9e6/browser.js#L15-L16

Contributor guide

No contributing guide indexed for this repository

Research direction

Start with browser.js at the linked implementation and reproduce the failure by using randombytes@2.1.0 in a browser environment built with Vite. Trace the browser-side crypto initialization and verify that the build no longer raises "global is not defined" while still accessing the browser crypto implementation.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
web-dev
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.