TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸TASIOMIND.DEV — OPERATIONAL▸▸▸FULL STACK DEVELOPER @ GWQ SERVICEPLUS AG▸▸▸FOUNDER — K8SGPT.AI▸▸▸OPEN SOURCE: ACTIVE▸▸▸DISTRIBUTED SYSTEMS / KUBERNETES / AI▸▸▸RUST + GO + PYTHON▸▸▸FIELD TESTED / STATUS — NOMINAL▸▸▸LOCATION: EUROPE/BERLIN▸▸▸

Working with TypeScript Modules in Netlify Edge Functions

January 1, 2024

Importing TS Files

Netlify Edge Functions use Deno as the runtime, so you'll follow Deno standards for importing/exporting files

  • In your import statements, you need to add the .ts file extension at the end of your filename. For example ./person.ts. File extensions are required when importing modules. All your imported files should have file extensions for their own imports too.
code
// Netlify Function
import Person, { sayHello } from "../someDir/person.ts";
code
// ../someDir/person.ts
import { randomFoo } from "./foo.ts"; // nested imports need to have file extensions too

export const sayHello;
export default Person;

Otherwise you will get similar to the following error:

code
TypeError: Module not found "file:///media/Files/foo/netlify/edge-functions/constants".
  • If you want to import normal TS files, they need to be outside the edge functions directory. Anything inside the edge functions directory is expected to be a module that has a default export and returns a function; i.e. be an edge function itself.

If you're trying to import another file in the edge functions directory, that file needs to be an edge function module, i.e. it should return a function. If you just want to have a basic TS file, for example for saving constant strings, move it outside the edge functions directory

code
// file: netlify/edge-functions/weather.ts
import { OWM_API_KEY, OWM_BASE_URL } from "../../constants.ts";
code
// file: src/constants.ts
export const OWM_API_KEY = Netlify.env.get("OPENWEATHER_API_KEY");
export const OWM_BASE_URL = `https://api.openweathermap.org/data/2.5/weather`;

Exporting Netlify Function modules

  • In your exported modules, you need a default export, and that default export must be a function.

Otherwise you will get similar to the following error:

code
◈ Failed to load Edge Function constants. The file does not seem to have a function as the default export.