atob for Node.js

2.1.2 · maintenance · verified Wed Apr 22

The `atob` package provides a Node.js implementation of the browser's `atob` (ASCII to Binary) function, which is used for decoding Base64-encoded strings. As of April 2026, the current stable version is 2.1.2, with the last significant update in 2018 (v2.1.0). Its release cadence appears to be very slow or effectively ceased, suggesting it's in a maintenance or stable state rather than active development. The primary differentiator of this package is its use of Node.js's native `Buffer` API to emulate the browser's exact functionality, allowing developers to decode Base64 strings in a server-side environment with behavior consistent with web browsers. A key consideration, explicitly noted by the library, is that it handles Unicode data incorrectly, mirroring the behavior of the browser's native `atob` function, which is designed for ASCII data. For proper Unicode and binary support, the README suggests alternative libraries like `unibabel.js`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates basic Base64 decoding of ASCII strings and highlights potential Unicode handling issues, showing how to import and use the `atob` function.

(function () {
  "use strict";

  // Import the atob function for decoding Base64 strings.
  // This package emulates the browser's atob behavior in Node.js.
  var atob = require('atob');

  // A simple Base64-encoded string representing "Hello, World!"
  var b64Ascii = "SGVsbG8sIFdvcmxkIQ==";
  var binAscii = atob(b64Ascii);
  console.log("Decoded ASCII:", binAscii); // Expected: "Hello, World!"

  // A Base64-encoded string that might contain non-ASCII characters (e.g., "你好")
  // Note: This package, like browser atob, may not handle Unicode correctly.
  // The actual encoding here assumes UTF-8 encoded bytes were Base64-encoded.
  var b64UnicodeExample = "5L2g5aW9"; // This is Base64 for '你好' (UTF-8 bytes)
  var binUnicodeAttempt = atob(b64UnicodeExample);
  console.log("Decoded Unicode (may be incorrect):", binUnicodeAttempt);
  // For correct Unicode, one would typically use Node's Buffer:
  // console.log("Correct Unicode (via Buffer):", Buffer.from(b64UnicodeExample, 'base64').toString('utf8'));

  // Another common use case: decoding URLs that might contain Base64 parameters
  // Assuming a fragment like 'data:text/plain;base64,VGhpcyBpcyBhIHRlc3Q='
  var urlEncodedB64 = "VGhpcyBpcyBhIHRlc3Q=";
  var decodedUrlContent = atob(urlEncodedB64);
  console.log("Decoded URL part:", decodedUrlContent);

  console.log("\nNote: atob is primarily for ASCII. For robust Unicode, use Buffer.from(b64, 'base64').toString('utf8').");
}());

view raw JSON →