Use Node.js APIs through polyfills
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.
You can import a Node.js API, such as node:buffer, in a function and let the Azion CLI build resolve it through polyfills for Azion Runtime. The build runs Azion 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.
Prerequisites
- An Azion account. To create one, refer to Create an account.
- The Azion CLI, installed and logged in to your account. To install it, refer to Azion CLI quickstart.
- Node.js 18 or later, with npm.
- A terminal and a code editor.
Create a JavaScript function project
The azion init command creates the project from a preset and a template. Create a project named 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:
At ? Choose a preset:, select Javascript.
At ? Choose a template:, select Hello World.
At Do you want to install project dependencies?, enter Y.
At Do you want to start a local development server?, enter n.
At Do you want to deploy your project?, enter n.
The command fetches the template, installs the dependencies, and confirms the project:
The remaining commands run from inside the project directory. Go to it:
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 …:
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.
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:
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
Run the function locally
The azion dev command builds the project with its polyfills and serves the function on your machine. Start it:
The command builds the function and serves it on port 3333:
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.
Open http://localhost:3333 in a browser. The function responds with the overwritten string:
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. For the list of APIs the bundler resolves through polyfills, refer to the Azion Bundler repository. Azion Bundler is open source, and you can propose presets and implementations there.
Deploy the function
The azion 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:
The command prints the domain of the workload:
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.