superagent-auth-bearer
raw JSON → 0.0.1 verified Sat Apr 25 auth: no javascript maintenance
A lightweight plugin for Superagent that adds a convenient `.authBearer()` method for OAuth2 Bearer Token authentication. Version 0.0.1, released in 2014, is a minimal wrapper that monkey-patches the Superagent prototype. It is not actively maintained and lacks TypeScript types, ESM support, or modern security features. Compared to alternatives like `superagent-oauth`, this plugin focuses solely on Bearer tokens without extra OAuth2 flows. It requires Superagent as a peer dependency and modifies the global request object.
Common errors
error TypeError: request(...).authBearer is not a function ↓
cause The plugin has not been initialized. It must be called with the superagent module.
fix
Make sure to run require('superagent-auth-bearer')(request) before using .authBearer().
error Cannot find module 'superagent-auth-bearer' ↓
cause The package may not be installed. Also check that you are in the correct directory.
fix
Run npm install superagent-auth-bearer --save (or --save-dev).
error Warning: require() of ES modules not supported ↓
cause You are using an ESM project and trying to require() a CommonJS module without proper handling.
fix
Use dynamic import or create a CommonJS compatibility layer. For example: const bearer = (await import('superagent-auth-bearer')).default;
Warnings
deprecated Package has not been updated since 2014 and likely has unpatched vulnerabilities. ↓
fix Consider using native superagent .set('Authorization', 'Bearer token') or migrate to a modern HTTP library like axios or node-fetch.
gotcha The plugin mutates the global superagent prototype, which can lead to unexpected behavior if multiple versions of superagent are installed. ↓
fix Ensure only one version of superagent is used; use npm's shrinkwrap or lockfile.
gotcha The plugin does not check for a valid token string; it sets the header even if token is null or undefined. ↓
fix Manually validate the token before passing to .authBearer().
breaking Incompatible with superagent v3+ which uses a different plugin API? ↓
fix Test with superagent v2; for v3+ use manual Authorization header.
Install
npm install superagent-auth-bearer yarn add superagent-auth-bearer pnpm add superagent-auth-bearer Imports
- plugin wrong
import 'superagent-auth-bearer';correctconst request = require('superagent'); require('superagent-auth-bearer')(request); - authBearer method wrong
request.get('/').set('Authorization', 'Bearer token')correctrequest.get('/').authBearer('token') - default export wrong
import bearer from 'superagent-auth-bearer';correctconst bearer = require('superagent-auth-bearer'); bearer(request);
Quickstart
const request = require('superagent');
require('superagent-auth-bearer')(request);
request
.get('https://api.example.com/data')
.authBearer(process.env.ACCESS_TOKEN ?? '')
.then(res => console.log(res.body))
.catch(err => console.error(err));