octokit / octokit.js

The all-batteries-included GitHub SDK for Browsers, Node.js, and Deno.
MIT License
6.93k stars 1.01k forks source link

[DOCS]: global fetch warning is confusing #2495

Closed skeddles closed 1 year ago

skeddles commented 1 year ago

Describe the need

Error: Global "fetch" not found. Please provide options.request.fetch to octokit or upgrade to node@18 or newer.

const { Octokit } = require("octokit");
const nodefetch = import("node-fetch");
const octokit = new Octokit({ auth: KEY, request: {fetch: nodefetch.fetch} });
const { Octokit } = require("octokit");
global.fetch = import("node-fetch").fetch;
const octokit = new Octokit({ auth: KEY, request: {fetch: fetch} });
const { Octokit } = require("octokit");
const {fetch}= import("node-fetch");
const octokit = new Octokit({ auth: KEY, request: {fetch: fetch} });

I've tried every combination I can think of, and can't get it to work. Please provide a working example of how I am supposed to make this work.

SDK Version

No response

API Version

No response

Relevant log output

Error: Global "fetch" not found. Please provide `options.request.fetch` to octokit or upgrade to node@18 or newer.
    at fetchWrapper (/home/skeddles/azlador.com/node_modules/@octokit/request/dist-node/index.js:57:11)
    at request2 (/home/skeddles/azlador.com/node_modules/@octokit/request/dist-node/index.js:172:14)
    at hook (/home/skeddles/azlador.com/node_modules/@octokit/auth-token/dist-node/index.js:58:10)
    at async requestWithGraphqlErrorHandling (/home/skeddles/azlador.com/node_modules/octokit/node_modules/@octokit/plugin-retry/dist-node/index.js:71:20)
    at async Job.doExecute (/home/skeddles/azlador.com/node_modules/bottleneck/light.js:405:18)

Code of Conduct

wolfy1339 commented 1 year ago

Please answer the following questions in order for us to help you:

skeddles commented 1 year ago

node v16.17 ^3.0.0 (latest) ^3.3.1 (latest)

wolfy1339 commented 1 year ago

That is the issue, we don't support Node JS 16 in version 3.x of Octokit

wolfy1339 commented 1 year ago

I agree that the message might be confusing, reopening to track a possible wording of the message

gr2m commented 1 year ago

you cannot combine require and import statements. If you use CommonJS (require), then install node-fetch@2 and try this

const { Octokit } = require("octokit");
const fetch = require("node-fetch");
const octokit = new Octokit({ auth: KEY, request: {fetch} });

But as @wolfy1339 mentioned, while this might work, we dropped official support for Node 16. Node 16 will no longer be maintained at all in two months: https://nodejs.dev/en/about/releases/.

wolfy1339 commented 1 year ago

Closing in favor of #2457