---
name: azion-use-node-js-apis-through-polyfills
description: >-
  Import a Node.js API such as node:buffer in a function, build it with polyfills through the Azion CLI, run it locally, and deploy it.
---

# Use Node.js APIs through polyfills

You can import a Node.js API, such as `node:buffer`, in a [function](/en/documentation/platform/functions/) and let the [Azion CLI](/en/documentation/devtools/cli/) build resolve it through polyfills for [Azion Runtime](/en/documentation/devtools/runtime/). The build runs [Azion Bundler](https://github.com/aziontech/bundler), which adds the polyfills when the project configuration allows them. Projects from the other presets the CLI offers, such as Next.js, React, Vue, Angular, and Astro, go through the same build, whether they start from `azion init` or join through `azion link`. To check which Node.js modules a function can use, refer to [Node.js APIs](/en/documentation/devtools/runtime/node/).

---

## Prerequisites

- An Azion account. To create one, refer to [Create an account](/en/documentation/fundamentals/creating-account/).
- The Azion CLI, installed and logged in to your account. To install it, refer to [Azion CLI quickstart](/en/documentation/devtools/cli/quickstart/).
- Node.js 18 or later, with npm.
- A terminal and a code editor.

---

## Create a JavaScript function project

The [azion init](/en/documentation/devtools/cli/init/) command creates the project from a preset and a template. Create a project named `polyfills-guide`:

```bash
azion init --name polyfills-guide
```

The command asks five questions. In each list, use the arrow keys or type to filter the entries. Answer them in this order:

1. **Select the Javascript preset**

   At `? Choose a preset:`, select *Javascript*.

2. **Select the Hello World template**

   At `? Choose a template:`, select *Hello World*.

3. **Install the dependencies**

   At `Do you want to install project dependencies?`, enter `Y`.

4. **Skip the local development server**

   At `Do you want to start a local development server?`, enter `n`.

5. **Skip the deploy**

   At `Do you want to deploy your project?`, enter `n`.

The command fetches the template, installs the dependencies, and confirms the project:

```text
? Choose a preset: Javascript
? Choose a template: Hello World

Fetching selected template...

Template successfully fetched
Template successfully configured
🤔 Do you want to install project dependencies? This may be required to generate initial configuration file (Y/n) Y
Installing application dependencies
up to date, audited 1 package in 307ms
found 0 vulnerabilities
…
[Azion] [Build] › ℹ  info      Using preset: javascript
[Azion] [Build] › ✔  success   Build completed successfully with only azion.config
[Azion] [IaC] › ✔  success   Manifest generated successfully at <project-dir>/.edge/manifest.json
🤔 Do you want to start a local development server? (y/N) n
If you want to start a local development server later, run 'azion dev'
Make sure to change to the new working directory before running building or deploying your project
🤔 Do you want to deploy your project? (y/N) n
If you want to deploy your application later, run 'azion deploy'
Make sure to change to the new working directory before running building or deploying your project
Your application polyfills-guide was initialized successfully
```

The remaining commands run from inside the project directory. Go to it:

```bash
cd polyfills-guide
```

The `polyfills-guide` directory holds the function project, with `index.js` as its entry file and `azion.config.mjs` as its configuration.

---

## Check the polyfills setting

The `polyfills` key of the `build` object in `azion.config.mjs` decides whether the build adds polyfills. It takes a boolean: `true` adds them, and `false` leaves them out. Polyfills are allowed by default. The file that `azion init` writes for the Javascript preset already sets the key to `true`. The excerpt shows the `build` block and cuts the rest of the file at `…`:

```javascript
export default {
  build: {
    preset: 'javascript',
    polyfills: true
  },
  …
}
```

Leave the file as it is to use a Node.js API in the next step. For every key the file accepts, refer to [azion.config.js](/en/documentation/devtools/cli/azion-config-js/).

The project builds with polyfills.

---

## Import a Node.js API in the function

The build uses `index.js` as the default entry of the Javascript preset. Replace the contents of `index.js` with the following function. It imports `Buffer` from `node:buffer`, encodes a string in two formats, and overwrites part of it:

```javascript
/**
 * An example of using the Node.js Buffer API in an Azion Function.
 * Build and run with the Azion CLI:
 * azion build
 * azion dev
 */
// Import the Buffer class from the node:buffer module
import { Buffer } from "node:buffer";

const main = async (event) => {
  // Create a buffer from the string "Hello Edge!" with UTF-8 encoding
  const helloBuffer = Buffer.from("Hello Edge!", "utf8");

  // Log the buffer content as a hexadecimal string
  console.log(helloBuffer.toString("hex"));
  // 48656c6c6f204564676521

  // Log the buffer content as a base64 string
  console.log(helloBuffer.toString("base64"));
  // SGVsbG8gRWRnZSE=

  // Overwrite 5 bytes at offset 6 with the string "World"
  helloBuffer.write("World", 6, 5, "utf8");

  // Log the updated buffer content as a string
  console.log(helloBuffer.toString());
  // Hello World

  // Return the buffer content as a response with a 200 status
  return new Response(helloBuffer.toString(), { status: 200 });
};

export default main;
```

The function logs three values with `console.log`, as its comments show, and answers each request with the overwritten string. For more `Buffer` operations, refer to [node:buffer](/en/documentation/devtools/runtime/node/buffer/).

---

## Run the function locally

The [azion dev](/en/documentation/devtools/cli/dev-command/) command builds the project with its polyfills and serves the function on your machine. Start it:

```bash
azion dev
```

The command builds the function and serves it on port 3333:

```text
…
[Azion] [Build] › ℹ  info      Using preset: javascript
[Azion] [Pre-Build] › ℹ  info      Starting pre-build...
[Azion] [Pre-Build] › ℹ  info      Pre-build completed successfully
[Azion] [Build] › ℹ  info      Using preset default entry: index.js
[Azion] [Build] › ⚠  warning   DEPRECATED: Migrate handler to → export default { fetch: (request, env, ctx) => {...} }
[Azion] [Build] › ℹ  info      Starting build...
[Azion] [Build] › ✔  success   Build completed successfully
[Azion] [Post-Build] › ℹ  info      Starting post-build...
[Azion] [Post-Build] › ✔  success   Post-build completed successfully
[Azion] [Server] › ✔  success   Function running on port 0.0.0.0:3333, url: http://localhost:3333
[Azion] [Server] › ℹ  info      Initial scan complete. Ready for changes.
```

The `DEPRECATED` warning refers to the `export default main` shape of the function, which still builds and runs. For the shape the build recommends, refer to [Handlers](/en/documentation/devtools/runtime/api-reference/handlers/).

Open `http://localhost:3333` in a browser. The function responds with the overwritten string:

```text
Hello World
```

The `Buffer` API runs locally through the polyfills. Stop the server in its terminal when you finish.

A module that resolves at build time can still throw `[unenv] <name> is not implemented yet!` when a function calls one of its methods. Before you use a module, check its status in [Node.js APIs](/en/documentation/devtools/runtime/node/). For the list of APIs the bundler resolves through polyfills, refer to the [Azion Bundler repository](https://github.com/aziontech/bundler). Azion Bundler is open source, and you can propose presets and implementations there.

---

## Deploy the function

The [azion deploy](/en/documentation/devtools/cli/deploy/) command uploads the project, builds it with the same polyfills setting, and creates the function, the application, and the workload. From the `polyfills-guide` directory, deploy the project:

```bash
azion deploy
```

The command prints the domain of the workload:

```text
Running deploy command
Uploading source files

Upload completed successfully!
Please visit https://console.azion.com/create/deploy/00000000-0000-0000-0000-000000000001 in case it did not open automatically, to follow up the full deploy process
Your Application was deployed successfully

To visualize your application access the Domain: https://xxxxxxxxxx.map.azionedge.net
Your application is being deployed to all Azion Locations and it might take a few minutes.
```

The first deploy can take several minutes to answer from every location; later deploys take about two minutes. Open the domain the command printed. The deployed function responds with `Hello World`, the same string as the local run.

For the flags that change how the command builds and uploads, such as `--auto`, `--dry-run`, and `--workers`, refer to [azion deploy](/en/documentation/devtools/cli/deploy/).

---

## Next steps

- [Node.js APIs](/en/documentation/devtools/runtime/node.md): Check which Node.js modules a function can use and how each one behaves.
- [node:buffer](/en/documentation/devtools/runtime/node/buffer.md): Convert encodings, combine buffers, and read request bodies as bytes.
- [azion.config.js](/en/documentation/devtools/cli/azion-config-js.md): Set the build, entry, and polyfills keys of your project configuration.
- [Handlers](/en/documentation/devtools/runtime/api-reference/handlers.md): Move the function to the handler shape the build recommends.
