Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

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

Closed
1 task done
konsumer opened this issue Jan 20, 2023 · 7 comments · Fixed by #563
Closed
1 task done

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

konsumer opened this issue Jan 20, 2023 · 7 comments · Fixed by #563
Labels
Type: Bug Something isn't working as documented

Comments

@konsumer
Copy link

konsumer commented Jan 20, 2023

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
@konsumer konsumer added Status: Triage This is being looked at and prioritized Type: Bug Something isn't working as documented labels Jan 20, 2023
@kfcampbell kfcampbell added Type: Support Any questions, information, or general needs around the SDK or GitHub APIs Priority: Normal Status: Up for grabs Issues that are ready to be worked on by anyone and removed Status: Triage This is being looked at and prioritized labels Jan 20, 2023
@damieng
Copy link

damieng commented Feb 18, 2023

Running into this exact problem trying to use octokit from Nuxt 3 as well.

@gr2m
Copy link
Contributor

gr2m commented Feb 18, 2023

Could you please test if this works correctly?

import { Octokit } from '@octokit-next/core'

@damieng
Copy link

damieng commented Feb 18, 2023

Yes, that is working perfectly, thank-you!

@wolfy1339 wolfy1339 removed the Type: Support Any questions, information, or general needs around the SDK or GitHub APIs label Mar 4, 2023
@Ddupasquier
Copy link

Having same issue in svelte.

@wolfy1339
Copy link
Member

As a workaround, for vite, svelte and similar environments, load the package from esm.sh:

import { Octokit } from "https://esm.sh/@octokit/core@4.2.1";

@zicklag
Copy link

zicklag commented May 25, 2023

I was able to fix this in my project ( SvelteKit + Vite ) by adding this to the vite config after installing isomorphic-fetch with pnpm:

  resolve: {
    alias: {
      'node-fetch': 'isomorphic-fetch',
    },
  },

Got the instructions from this StackOverflow thread, which also mentioned you might need to add:

define: {
  global: {}
}

@wolfy1339
Copy link
Member

This will get resolved by octokit/request.js#586

@wolfy1339 wolfy1339 removed the Status: Up for grabs Issues that are ready to be worked on by anyone label May 27, 2023
@wolfy1339 wolfy1339 linked a pull request Jun 20, 2023 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Something isn't working as documented
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

7 participants