> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/nodejs/userland-migrations/llms.txt
> Use this file to discover all available pages before exploring further.

# buffer.atob() & buffer.btoa() Migration

> Migrate from legacy buffer.atob() and buffer.btoa() APIs to modern Buffer methods for base64 encoding and decoding

This recipe migrates usage of the legacy `buffer.atob()` and `buffer.btoa()` APIs to the current recommended Buffer-based approaches for base64 encoding and decoding.

## What It Does

The codemod transforms:

* `buffer.atob(data)` → `Buffer.from(data, 'base64').toString('binary')`
* `buffer.btoa(data)` → `Buffer.from(data, 'binary').toString('base64')`

## Usage

```bash theme={null}
npx codemod nodejs/buffer-atob-btoa
```

## Examples

### Migrating buffer.atob()

Decoding base64-encoded data:

<CodeGroup>
  ```javascript Before theme={null}
  const buffer = require('node:buffer');
  const data = 'SGVsbG8gV29ybGQh'; // "Hello World!" in base64
  const decodedData = buffer.atob(data);
  console.log(decodedData); // Outputs: Hello World!
  ```

  ```javascript After theme={null}
  const data = 'SGVsbG8gV29ybGQh'; // "Hello World!" in base64
  const decodedData = Buffer.from(data, 'base64').toString('binary');
  console.log(decodedData); // Outputs: Hello World!
  ```
</CodeGroup>

### Migrating buffer.btoa()

Encoding data to base64:

<CodeGroup>
  ```javascript Before theme={null}
  const buffer = require('node:buffer');
  const data = 'Hello World!';
  const encodedData = buffer.btoa(data);
  console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh
  ```

  ```javascript After theme={null}
  const data = 'Hello World!';
  const encodedData = Buffer.from(data, 'binary').toString('base64');
  console.log(encodedData); // Outputs: SGVsbG8gV29ybGQh
  ```
</CodeGroup>

## Why Migrate?

The `buffer.atob()` and `buffer.btoa()` methods were convenience wrappers around Buffer operations. Using Buffer methods directly:

* Provides more explicit control over encoding
* Aligns with modern Node.js best practices
* Reduces dependency on the `buffer` module import
* Offers better performance in some scenarios

<Tip>
  The Buffer API provides more encoding options beyond 'base64' and 'binary', including 'hex', 'utf8', 'ascii', and more.
</Tip>

## References

* [Node.js Buffer Documentation](https://nodejs.org/api/buffer.html)
