octokit-from-auth simplifies the instantiation of a GitHub Octokit client, abstracting away the complexities of authentication token resolution. It automatically attempts to retrieve a token from `process.env.GH_TOKEN` or by executing `gh auth token` via the `get-github-auth-token` package. The library offers two functions: `octokitFromAuth`, which rejects if no token is found, and `octokitFromAuthSafe`, which resolves to an unauthenticated Octokit instance if no token is available. The current stable version is 0.3.2. Releases are consistent, primarily for dependency updates and minor fixes, with new features introduced in minor versions. It ships with TypeScript types and requires Node.js >=18.3.0. This package differentiates itself by offering a robust, opinionated, and flexible approach to Octokit instantiation and authentication, minimizing boilerplate for interacting with the GitHub API.
npm install octokit-from-authVerified import paths — ran on the pinned version, not inferred.
Demonstrates creating both authenticated and unauthenticated Octokit instances using `octokitFromAuth` and `octokitFromAuthSafe`, handling potential authentication failures gracefully.
Use `octokitFromAuthSafe` for scenarios where an unauthenticated Octokit is acceptable, or ensure `process.env.GH_TOKEN` is set or `gh auth token` is configured and available.
Ensure your project uses `type: "module"` in `package.json` or uses `.mjs` files for ESM, and upgrade your Node.js runtime to `18.3.0` or newer.
Consult the changelog for `@octokit/core` (or the specific Octokit package you use) for `v5` to understand potential breaking changes in the underlying API. Update any code directly calling Octokit methods or properties that might have changed.
Convert your consuming script to an ES module by using `import` statements and ensuring your `package.json` specifies `"type": "module"` or by renaming your file to `.mjs`.
Set the `GH_TOKEN` environment variable (e.g., `GH_TOKEN=gho_... node my-script.js`), or ensure the GitHub CLI (`gh`) is installed and authenticated, or explicitly pass an `auth` option to `octokitFromAuth({ auth: 'YOUR_TOKEN' })`.