sandoche / Github-contributors-file

A javascript library to get individual file contributors from Github
MIT License
0 stars 0 forks source link

ReferenceError: fetch is not defined #9

Open zeke opened 4 years ago

zeke commented 4 years ago

Hi there, @sandoche đź‘‹

Just giving this library a try. I'm getting this error:

(node:26730) UnhandledPromiseRejectionWarning: ReferenceError: fetch is not defined
    at /Users/z/git/github/help-docs/node_modules/github-contributors-file/lib/github-contributors-file.js:118:7
    at new Promise (<anonymous>)
    at getContributors (/Users/z/git/github/help-docs/node_modules/github-contributors-file/lib/github-contributors-file.js:115:10)
    at main (/Users/z/git/github/help-docs/script/get-contributors.js:10:30)
    at Object.<anonymous> (/Users/z/git/github/help-docs/script/get-contributors.js:7:1)
    at Module._compile (internal/modules/cjs/loader.js:868:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:879:10)
    at Module.load (internal/modules/cjs/loader.js:731:32)
    at Function.Module._load (internal/modules/cjs/loader.js:644:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:931:10)
(node:26730) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:26730) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

I suspect this is because all the dependencies are declared as devDependencies, so they're not installed when the package is npm installed.

sandoche commented 4 years ago

I think it's due to the fact I am using fetch without node-fetch. Can you try to fork the library and to add node-fetch ?

zeke commented 4 years ago

I ended up rolling my own solution for this using octokit:

require('dotenv-safe').config()

const { Octokit } = require('@octokit/core')
const { paginateRest } = require('@octokit/plugin-paginate-rest')
const MyOctokit = Octokit.plugin(paginateRest)
const octokit = new MyOctokit({ auth: process.env.GITHUB_TOKEN })

// Hit the REST API to collect contributor data for an individual file.
async function getContributorsForFile (owner, repo, path) {

  // paginate all contribution to this file
  const contributors = await octokit.paginate('GET /repos/:owner/:repo/commits', {
    owner,
    repo,
    path,
    per_page: 100
  })

  return contributors
}

I will likely not use this library again but wanted to give you a heads up. Feel free to close this issue if you like.

sandoche commented 4 years ago

Well done ! I will leave it open for other people who have the same problems.