Skip to content

[BUG]: Uncaught ReferenceError: global is not defined #547

@konsumer

Description

@konsumer

What happened?

If you setup a fresh vite site, and run npm i @octokit/core then import it with:

import { Octokit } from '@octokit/core'

You will see in web dev-console:

Uncaught ReferenceError: global is not defined
    at index.mjs:657:16

It appears to come from node-fetch, which should not be running in this web-context.

Screenshot 2023-01-19 at 11 20 28 PM

(judging by the code near this in the console)

In my top-level index.html, I can do this:

<script type="module">
    import { Octokit } from "https://cdn.skypack.dev/octokit";
    window.Octokit = Octokit
    </script>

and call it like this:

const octokit = new window.Octokit({ auth })

and it works fine, but this is not really standard frontend library usage (should be able to import it, directly.)

I recommend using cross-fetch which ponyfills more correctly on node environments and works correctly on frontend:

import fetch from 'cross-fetch'

It uses node-fetch, but wraps it correctly.

Versions

node 17.6.0, 18.5.0
vite 4.0.4
@octokit/core: 4.1.0

Relevant log output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type: BugSomething isn't working as documented

    Type

    No type

    Projects

    Status

    ✅ Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions