How to fix Error: Invalid base64 padding in Node.js

Node.jsINTERMEDIATEMEDIUMUpdated Dec 9, 2025· 151 views

This error occurs when attempting to decode a base64 string that has incorrect padding. Base64 encoding requires padding to a multiple of 4 characters using the "=" character, and improper padding causes decoding failures in strict validation contexts.

Quick fix

Fixes ERR_INVALID_BASE64_PADDING

Input bytes → Output chars → Padding
1 byte (8 bits) → 2 chars → 2 equals (==)
2 bytes (16 bits) → 3 chars → 1 equals (=)  [Note: 1 padding is valid for output]
3 bytes (24 bits) → 4 chars → 0 equals
  1. Understand base64 padding rules
  2. Verify base64 string length and padding
  3. Fix incorrect padding
  4. Handle base64url encoding (JWT/web)
node.js — error
Input bytes → Output chars → Padding
1 byte (8 bits) → 2 chars → 2 equals (==)
2 bytes (16 bits) → 3 chars → 1 equals (=) [Note: 1 padding is valid for output]
3 bytes (24 bits) → 4 chars → 0 equals
Error: Invalid base64 padding

What this error means

Base64 is an encoding scheme that represents binary data using 64 printable ASCII characters (A-Z, a-z, 0-9, +, /). To ensure the encoded string is always a multiple of 4 characters, padding is added using the "=" character. When Node.js or other libraries encounter a base64 string with invalid padding, they throw an error because the string violates base64 formatting rules. Valid base64 strings must: - Have a length that is a multiple of 4 (including padding) - Use exactly 0, 2, or 3 equals signs for padding - Never use 1 equals sign (which is invalid padding) This error commonly appears when working with cryptographic libraries, JWT tokens, data URLs, or any code that decodes base64-encoded data with strict validation enabled.

Typical symptoms

  • Buffer.from() throws 'Invalid base64 padding' when decoding certain strings
  • JWT library rejects tokens with improper base64url encoding
  • Crypto operations fail when decoding base64-encoded keys or certificates
  • Data URLs with base64 content fail to decode in Node.js applications
  • Error occurs intermittently with data from external APIs or files
  • Manual base64 string concatenation or manipulation results in padding errors

Common causes

  1. Base64 string has only 1 equals sign at the end (invalid - must be 0, 2, or 3)
  2. String length is not a multiple of 4 characters without proper padding
  3. Data was truncated or corrupted during transmission, removing padding characters
  4. Manually constructing base64 strings without understanding padding requirements
  5. Using base64url encoding (for JWT/web) where '=' is stripped but not properly handled
  6. Copying base64 strings from different sources with inconsistent padding conventions
  7. Decoding libraries have strict validation enabled for security purposes

How to fix "Error: Invalid base64 padding"

1Understand base64 padding rules

Base64 encodes data in groups of 3 bytes (24 bits) into 4 characters (6 bits each). The padding depends on the input length:

bash
Input bytes → Output chars → Padding
1 byte (8 bits) → 2 chars → 2 equals (==)
2 bytes (16 bits) → 3 chars → 1 equals (=)  [Note: 1 padding is valid for output]
3 bytes (24 bits) → 4 chars → 0 equals

Valid base64 strings after padding are:
- Length % 4 == 0 (always a multiple of 4)
- Ends with 0, 2, or 3 equals signs (1 equals sign is always invalid)

javascript
const valid1 = "dGVzdA==";    // 6 chars with 2 padding - valid
const valid2 = "dGVzd===";    // 8 chars with 3 padding - valid
const valid3 = "dGVzd";       // 5 chars with 0 padding - valid (lenient decoders)
const invalid = "dGVzd=";     // 6 chars with 1 padding - INVALID
2Verify base64 string length and padding

Check that your base64 string has correct padding before decoding:

javascript
function validateBase64(str) {
  // Check if string has invalid padding (1 equals sign)
  const paddingMatch = str.match(/=+$/);
  const paddingCount = paddingMatch ? paddingMatch[0].length : 0;

  if (paddingCount === 1) {
    console.error("Invalid base64 padding: has 1 equals sign");
    return false;
  }

  // Check if length is multiple of 4 (after padding)
  if ((str.length + paddingCount) % 4 !== 0) {
    console.error("Invalid base64 length: not a multiple of 4");
    return false;
  }

  return true;
}

const base64String = "dGVzd=";  // Invalid
if (validateBase64(base64String)) {
  const buffer = Buffer.from(base64String, "base64");
  console.log(buffer);
} else {
  console.error("Base64 string has invalid padding");
}
3Fix incorrect padding

If your base64 string has incorrect padding, fix it before decoding:

javascript
function fixBase64Padding(str) {
  // Remove all trailing equals signs
  let cleaned = str.replace(/=+$/, "");

  // Add correct padding based on string length
  const padding = (4 - (cleaned.length % 4)) % 4;
  return cleaned + "=".repeat(padding);
}

// Examples
console.log(fixBase64Padding("dGVzd="));      // "dGVzd===" -> may be over-padded
console.log(fixBase64Padding("dGVz"));        // "dGVz=" (adds 1) or "dGVz==" depending on content

// Better approach: don't add padding if it's optional
const str = "dGVzd=";
const cleaned = str.replace(/=+$/, "");
const buffer = Buffer.from(cleaned, "base64");
console.log(buffer.toString("utf-8")); // Works with modern Node.js

Note: Modern versions of Node.js are lenient with padding, but older versions and strict decoders require exact padding.

4Handle base64url encoding (JWT/web)

Base64url is used in URLs and JWTs where "=" padding is stripped. When decoding base64url, add padding back:

javascript
function decodeBase64Url(str) {
  // Replace base64url characters with standard base64
  let base64 = str.replace(/-/g, "+").replace(/_/g, "/");

  // Add padding if needed
  const padding = (4 - (base64.length % 4)) % 4;
  base64 += "=".repeat(padding);

  return Buffer.from(base64, "base64").toString("utf-8");
}

// JWT header example (typically base64url)
const jwtHeader = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9";
console.log(decodeBase64Url(jwtHeader));
// Output: {"alg":"HS256","typ":"JWT"}

Base64url uses "-" and "_" instead of "+" and "/" to be URL-safe.

5Validate and sanitize external base64 input

When receiving base64 strings from external sources (APIs, files, user input), validate them:

javascript
function safeBase64Decode(str) {
  // Trim whitespace
  str = str.trim();

  // Check for invalid characters
  if (!/^[A-Za-z0-9+/]*={0,3}$/.test(str)) {
    throw new Error("Base64 string contains invalid characters");
  }

  // Check padding
  const paddingCount = (str.match(/=/g) || []).length;
  if (paddingCount === 1) {
    throw new Error("Base64 has invalid padding (1 equals sign)");
  }

  // Validate total length
  if ((str.length) % 4 !== 0) {
    throw new Error("Base64 length must be multiple of 4");
  }

  try {
    return Buffer.from(str, "base64");
  } catch (e) {
    throw new Error("Failed to decode base64: " + e.message);
  }
}

// Usage
try {
  const buffer = safeBase64Decode("dGVzdA==");
  console.log("Decoded:", buffer.toString("utf-8"));
} catch (err) {
  console.error("Invalid base64:", err.message);
}
6Use libraries with lenient decoding when needed

If you need to handle base64 strings with various padding styles, use libraries or enable lenient mode:

javascript
// For lenient decoding, you can remove padding entirely
function lenientBase64Decode(str) {
  // Remove all padding
  const cleaned = str.replace(/=+$/, "");

  // Modern Node.js handles this fine
  try {
    return Buffer.from(cleaned, "base64").toString("utf-8");
  } catch (e) {
    // Fallback: add proper padding
    const padded = cleaned + "=".repeat((4 - cleaned.length % 4) % 4);
    return Buffer.from(padded, "base64").toString("utf-8");
  }
}

// For JSON Web Tokens, use libraries
const jwt = require("jsonwebtoken");
const decoded = jwt.decode(token); // Handles base64url automatically

// For general base64 with various padding, handle both styles
function flexBase64Decode(str) {
  try {
    return Buffer.from(str, "base64");
  } catch (e) {
    // Try with padding fixed
    const padded = str + "=".repeat((4 - str.length % 4) % 4);
    return Buffer.from(padded, "base64");
  }
}

Advanced notes

Base64 Standard (RFC 4648): The standard specifies that padding with "=" is REQUIRED for proper base64 encoding. However, RFC 4648 Section 3.1 defines base64url which is used in URLs and JWTs where padding is often omitted. Node.js is lenient and accepts both padded and unpadded base64, but strict decoders (especially older versions or security-focused libraries) reject invalid padding. Why 1 Equals Sign is Invalid: In base64 encoding, every 3 input bytes produce 4 output characters. If you have remainder bytes: 0 remainder produces 4 chars with 0 padding, 1 remainder produces 2 chars with 2 padding, 2 remainder produces 3 chars with 1 padding. A single "=" always indicates incomplete/truncated data and breaks the 4-character alignment rule. Padding in Different Contexts: Standard Base64 requires padding (RFC 4648), Base64URL (JWT/URLs) has optional padding but when present must be correct, Data URLs always include proper padding, and Crypto Keys typically have strict validation enforced. Security Note: Invalid base64 padding can indicate corrupted or tampered data. In cryptographic contexts, always validate padding strictly and reject invalid strings rather than attempting repair. Performance: For high-volume base64 decoding, pre-validate strings to catch padding errors early. Catching errors during Buffer.from() is more expensive than regex validation. Compatibility: Node.js v14+ are more lenient than earlier versions with base64 padding. If supporting older versions or strict parsers, always ensure proper padding.

Frequently asked questions

What does "ERR_INVALID_BASE64_PADDING" mean?
Base64 is an encoding scheme that represents binary data using 64 printable ASCII characters (A-Z, a-z, 0-9, +, /). To ensure the encoded string is always a multiple of 4 characters, padding is added using the "=" character. js or other libraries encounter a base64 string with invalid padding, they throw an error because the string violates base64 formatting rules.
How do I fix "ERR_INVALID_BASE64_PADDING"?
This error occurs when attempting to decode a base64 string that has incorrect padding. Base64 encoding requires padding to a multiple of 4 characters using the "=" character, and improper padding causes decoding failures in strict validation contexts.
What causes "ERR_INVALID_BASE64_PADDING"?
Base64 string has only 1 equals sign at the end (invalid - must be 0, 2, or 3) String length is not a multiple of 4 characters without proper padding Data was truncated or corrupted during transmission, removing padding characters
Is the "ERR_INVALID_BASE64_PADDING" error serious?
It is a medium-severity error — more of a blocker than a risk. The steps above resolve it.

Official resources & further reading