Skip to content

global Reference Error in Browser Environment #38

Open
@0xharshrastogi

Description

@0xharshrastogi

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

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:

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

As the source code mentioned below crypto is defined correctly

randombytes/browser.js

Lines 15 to 16 in b4eb412

const _global = typeof globalThis !== 'undefined' ? globalThis : global
const crypto = _global.crypto || _global.msCrypto

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions