How to resolve Node.js APIs through polyfills
Learn how to configure and use Node.js APIs through polyfills.
Through Azion CLI, 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.
The projects built with these frameworks typically use Node.js APIs. Through the Azion build process, carried by Azion 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 with Functions enabled.
- The latest version of Azion CLI installed.
- Code editor.
- Access to the terminal.
- Node.js ≥ 18.
Initializing a JavaScript project on the edge
- On the terminal, initialize the project:
- Enter the name
polyfills-guide:
- Choose the JavaScript preset:
- Choose the
Hello Worldtemplate:
-
Enter
yto start a local development server. -
Enter
yto install project dependencies. -
Access the port that was returned in the terminal. Example:
-
Go back to the terminal and terminate the process.
-
Access your project:
- Open the
azion.config.jsfile. It’ll look like this:
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:
In our example, you can leave the file as it is. Polyfills are allowed by default.
- After applying these settings, you can import the necessary APIs into your project. This example uses the Node.js Buffer API:
Inside main.js:
- Run the project locally by running:
Now you can check the logs in the terminal and see the Buffer API working through polyfills.
Deploy the function to Azion
After testing locally with azion dev, deploy your function to the Azion Platform:
- Run the deploy command:
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: