redis/node-redis · error · TypeError

Invalid pathname

Error message

Invalid pathname

What it means

For redis:// and rediss:// URLs the optional pathname after the leading slash is interpreted as the logical database index and must be numeric (e.g. redis://h:6379/0). A non-numeric pathname like /foo or /db/0 is rejected.

Solutions

  1. Drop the pathname entirely (defaults to db 0), or
  2. Use a single integer in [0,15] (or whatever the server's databases config allows).

Example fix

// before
createClient({ url: 'redis://host:6379/main' });

// after
createClient({ url: 'redis://host:6379/0' });
Defensive patterns

Strategy: validation

Validate before calling

function assertRedisPathname(url) {
  const u = new URL(url);
  if (u.pathname.length > 1 && !/^\/\d+$/.test(u.pathname)) {
    throw new TypeError(`Invalid Redis db pathname ${u.pathname}; expected /<integer>`);
  }
}

Prevention

When it happens

Trigger: Passing redis://host:6379/foo or redis://host:6379/some/path to createClient.

Common situations: Mistakenly appending a REST-style path; confusing a Redis URL with an HTTP one; copy-paste error.

Related errors


AI-assisted analysis of redis/node-redis@90fd0652bc (2026-08-11). Data as JSON: /api/errors/f096675c4ed64675. Report an issue: GitHub.

Appendix: source

Thrown at packages/client/lib/client/index.ts:524

    if (password) {
      parsed.password = decodeURIComponent(password);
    }

    if (username || password) {
      parsed.credentialsProvider = {
        type: 'async-credentials-provider',
        credentials: async () => (
          {
            username: username ? decodeURIComponent(username) : undefined,
            password: password ? decodeURIComponent(password) : undefined
          })
      };
    }

    if (pathname.length > 1) {
      const database = Number(pathname.substring(1));
      if (isNaN(database)) {
        throw new TypeError('Invalid pathname');
      }

      parsed.database = database;
    }

    return parsed;
  }

  static #parseUnixURL(url: string): AnyRedisClientOptions & {
    socket: Exclude<AnyRedisClientOptions['socket'], undefined> & {
      tls: boolean
    }
  } {
    // unix://[user[:password]@]/path/to/sock[?db=N]
    const match = /^unix:\/\/(?:([^:@/]*)(?::([^@/]*))?@)?(\/[^?#]*)(?:\?([^#]*))?(?:#.*)?$/.exec(url);
    if (!match || match[3] === '/') {
      throw new TypeError('Invalid unix URL');
    }

View on GitHub (pinned to 90fd0652bc)