Notes

Working with TypeScript Modules in Netlify Edge Functions

Imports for TypeScript modules for Edge Functions are different because of the Deno runtime environment

Edit on GitHub

Typescript
2 minutes

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.
1// Netlify Function
2import Person, { sayHello } from "../someDir/person.ts";
1// ../someDir/person.ts
2import { randomFoo } from "./foo.ts"; // nested imports need to have file extensions too
3
4export const sayHello
5export default Person

Otherwise you will get similar to the following error:

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

1// file: netlify/edge-functions/weather.ts
2import { OWM_API_KEY, OWM_BASE_URL } from "../../constants.ts"
1// file: src/constants.ts
2export const OWM_API_KEY = Netlify.env.get("OPENWEATHER_API_KEY")
3export 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:

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

Related