# How to resolve Node.js APIs through polyfills

Through [Azion CLI](/en/documentation/products/azion-cli/overview/), you can initialize an application based on starter templates or link an existing project. The list of supported web frameworks includes Next.js, React, Vue, Angular, Astro, JavaScript itself, and others. These JavaScript frameworks run at Azion's edge, on top of [Azion Runtime](/en/documentation/runtime/overview/).

The projects built with these frameworks typically use Node.js APIs. Through the Azion build process, carried by [Azion Bundler](https://github.com/aziontech/bundler), these APIs are resolved through the use of *polyfills*. 

This guide will show how to use **Node.js Buffer API** through polyfills in a JavaScript project by using Azion CLI and Azion Bundler.

---

## Requirements

Before getting started, you must have:

- An [Azion platform account](/en/documentation/products/accounts/creating-account/) with [Functions](/en/documentation/products/build/applications/functions/) enabled.
- [The latest version of Azion CLI installed](/en/documentation/products/azion-cli/overview/).
- Code editor.
- Access to the terminal.
- Node.js ≥ 18.

---

## Initializing a JavaScript project on the edge

1. On the terminal, initialize the project:

```sh 
  azion init
```

2. Enter the name `polyfills-guide`:

```sh 
Your application s name:  (glorious-curiosity) polyfills-guide
```

3. Choose the JavaScript preset: 

:::tip
You can start typing the preset's name to filter the results.
:::

```sh 
? Choose a preset:  [Use arrows to move, type to filter]
  Angular
  Astro
  Docusaurus
  Eleventy
  Emscripten
  Gatsby
  Hexo
  Hono
  Hugo
> Javascript
  Jekyll
  ...
```

4. Choose the `Hello World` template:

:::tip
You can start typing the template's name to filter the results.
:::

```sh
? Choose a template:  [Use arrows to move, type to filter]
  Azion SQL Database
  Drizzle + Neon Sample
  Drizzle + TiDB Sample
  Drizzle + Turso Sample
  Function GitHub AutoDeploy
  Fauna DB Boilerplate
> Hello World
  HTMX Boilerplate
  Simple Javascript Router Node
  ...
```

5. Enter `y` to start a local development server. 

6. Enter `y` to install project dependencies.  

7. Access the port that was returned in the terminal. Example:

```bash 
[Azion Bundler] [Server] › ✔  success   Function running on port 0.0.0.0:3333, url: http://localhost:3333
``` 

8. Go back to the terminal and terminate the process.

9. Access your project: 

```bash
cd polyfills-guide
```

10. Open the `azion.config.js` file. It'll look like this:

```js title="polyfills-guide/azion.config.js"
import { defineConfig } from 'azion';

export default defineConfig({
  build: {
    entry: 'main.js',
    preset: {
      name: 'javascript',
    },
  },
});
```

This is the file where you can add specific configurations for your project. For example, the `polyfills` property can be set to `true` or `false` to control whether or not to allow the use of polyfills:

```js title="polyfills-guide/azion.config.js"
import { defineConfig } from 'azion';

export default defineConfig({
  build: {
    entry: 'main.js',
    preset: {
      name: 'javascript',
    },
    polyfills: true, // allows the use of polyfills
  },
});
```

In our example, you can leave the file as it is. Polyfills are allowed by default.

:::note
For more information about configuring your project, check the [azion.config.js file reference](/en/documentation/devtools/cli/configs/azion-config-js/).
:::

11. After applying these settings, you can import the necessary APIs into your project. This example uses the Node.js Buffer API:

**Inside main.js**:

```js title="polyfills-guide/main.js"
// Import the Buffer class from the 'buffer' module in Node.js
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"));
  // Expected output: 48656c6c6f204564676521

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

  // Overwrite part of the buffer with the string "World" at the specified offset and length
  helloBuffer.write("World", 6, 5, "utf8");

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

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

export default main;
```

12. Run the project locally by running: 

```bash 
azion dev
```

Now you can check the logs in the terminal and see the Buffer API working through polyfills. 

:::tip
You can access a list of APIs resolved through polyfills by Azion Bundler build on [its repository](https://github.com/aziontech/bundler) and on [Azion's compatibility documentation](/en/documentation/products/azion-edge-runtime/compatibility/node/). Azion Bundler's an **open-source** project and you can propose new presets and implementations.
:::

---

## Deploy the function to Azion

After testing locally with `azion dev`, deploy your function to the Azion Platform:

13. Run the deploy command:

```bash
azion deploy
```

The CLI builds the project, applies the configured polyfills, and publishes the function to the platform. At the end of the process, you'll receive the URL to access your application.

### Useful deploy flags

| Flag | Description |
|------|-------------|
| `--auto` | Runs the complete build, provisioning, and distribution flow without interruptions or interactive prompts. |
| `--workers <number>` | Sets the number of parallel workers for uploading static files to Object Storage. By default, it's automatically calculated based on available CPU cores (maximum: 20). |
| `--dry-run` | Simulates the deploy process locally, validating the generated routing rules without affecting production resources. |

**Example with flags:**

```bash
azion deploy --auto --workers 10
```

:::note
The build process applies polyfills automatically based on the `azion.config.js` file configuration. Make sure the required modules are listed in the `polyfills` property before deploying.
:::