jaredhanson/passport · error · Error

Authentication strategies must have a name

Error message

Authentication strategies must have a name

What it means

Passport's Authenticator#use() registers a strategy under a name. If no name is supplied, Passport falls back to the strategy instance's own .name property (e.g. the name passed to the strategy's constructor). If both are missing/undefined, it throws this error because strategies are keyed by name and cannot be looked up later.

Source

Thrown at lib/authenticator.js:62

 * @param {string} [name=strategy.name] - Name of the strategy.  When specified,
 *          this value overrides the strategy's name.
 * @param {Strategy} strategy - Authentication strategy.
 * @returns {this}
 *
 * @example <caption>Register strategy.</caption>
 * passport.use(new GoogleStrategy(...));
 *
 * @example <caption>Register strategy and override name.</caption>
 * passport.use('password', new LocalStrategy(function(username, password, cb) {
 *   // ...
 * }));
 */
Authenticator.prototype.use = function(name, strategy) {
  if (!strategy) {
    strategy = name;
    name = strategy.name;
  }
  if (!name) { throw new Error('Authentication strategies must have a name'); }
  
  this._strategies[name] = strategy;
  return this;
};

/**
 * Deregister a strategy that was previously registered with the given name.
 *
 * In a typical application, the necessary authentication strategies are
 * registered when initializing the app and, once registered, are always
 * available.  As such, it is typically not necessary to call this function.
 *
 * @public
 * @param {string} name - Name of the strategy.
 * @returns {this}
 *
 * @example
 * passport.unuse('acme');

View on GitHub (pinned to 217018dbc4)

Solutions

  1. Pass an explicit name as the first argument: passport.use('local', new LocalStrategy(...))
  2. Ensure the strategy instance exposes a .name property (most official strategies set it in their constructor)
  3. Log/inspect the value passed to passport.use to confirm the import is not undefined
  4. Verify the strategy package version and constructor API match the docs

Example fix

// before
const LocalStrategy = require('passport-local').Strategy;
passport.use(new LocalStrategy(verify)); // LocalStrategy undefined -> no .name
// after
const LocalStrategy = require('passport-local').Strategy;
passport.use('local', new LocalStrategy(verify));
Defensive patterns

Strategy: validation

Validate before calling

function validateStrategyRegistration(name, strategy) {
  const s = strategy || name;
  const n = strategy ? name : (s && s.name);
  if (typeof n !== 'string' || n.length === 0) {
    throw new TypeError('Strategy must have a name before passport.use()');
  }
}

Type guard

function hasStrategyName(s) {
  return typeof s === 'object' && s !== null && typeof s.name === 'string' && s.name.length > 0;
}

Try / catch

try {
  passport.use(new LocalStrategy(opts, verify));
} catch (err) {
  if (err.message.includes('strategies must have a name')) {
    passport.use('local', new LocalStrategy(opts, verify));
  } else { throw err; }
}

Prevention

When it happens

Trigger: calling passport.use(new SomeStrategy({...})) where the strategy instance has no .name property, or passport.use(undefined/null) e.g. due to a failed import or a factory returning undefined.

Common situations: Typo'd require/import so the strategy constructor is undefined; using a custom strategy class that never calls super(name) or sets this.name; upgrading a strategy package where the constructor signature changed and the name argument was dropped.

Understand the failure class

Related errors


AI-assisted analysis of jaredhanson/passport@217018dbc4 (2026-08-31). Data as JSON: /api/errors/68fbe463ba43e979. Report an issue: GitHub.