Edge Functions

Managing dependencies

Handle dependencies within Edge Functions.


Importing dependencies

Supabase Edge Functions support several ways to import dependencies:

1
2
3
4
5
6
7
8
// NPM packages (recommended)import { createClient } from 'npm:@supabase/supabase-js@2'// Node.js built-insimport process from 'node:process'// JSR modules (Deno's registry)import path from 'jsr:@std/path@1.0.8'

Each function should have its own deno.json file to manage dependencies and configure Deno-specific settings. This ensures proper isolation between functions and is the recommended approach for deployment. When you update the dependencies for one function, it won't accidentally break another function that needs different versions.

1
2
3
4
5
6
{ "imports": { "supabase": "npm:@supabase/supabase-js@2", "lodash": "https://cdn.skypack.dev/lodash" }}

You can add this file directly to the function’s own directory:

1
2
3
4
5
6
7
8
9
└── supabase ├── functions ├── function-one ├── index.ts └── deno.json # Function-specific Deno configuration └── function-two ├── index.ts └── deno.json # Function-specific Deno configuration └── config.toml

Using import maps (legacy)

Import Maps are a legacy way to manage dependencies, similar to a package.json file. While still supported, we recommend using deno.json. If both exist, deno.json takes precedence.

Each function should have its own import_map.json file for proper isolation:

1
2
3
4
5
6
# /function-one/import_map.json{ "imports": { "lodash": "https://cdn.skypack.dev/lodash" }}

This JSON file should be located within the function’s own directory:

1
2
3
4
5
└── supabase ├── functions ├── function-one ├── index.ts └── import_map.json # Function-specific import map

If you’re using import maps with VSCode, update your .vscode/settings.json to point to your function-specific import map:

1
2
3
4
5
{ "deno.enable": true, "deno.unstable": ["bare-node-builtins", "byonm"], "deno.importMap": "./supabase/functions/function-one/import_map.json"}

You can override the default import map location using the --import-map <string> flag with serve and deploy commands, or by setting the import_map property in your config.toml file:

1
2
[functions.my-function]import_map = "./supabase/functions/function-one/import_map.json"

Private NPM packages

To use private npm packages, create a .npmrc file within your function’s own directory.

1
2
3
4
5
6
└── supabase └── functions └── my-function ├── index.ts ├── deno.json └── .npmrc # Function-specific npm configuration

Add your registry details in the .npmrc file. Follow this guide to learn more about the syntax of npmrc files.

1
2
3
# /my-function/.npmrc@myorg:registry=https://npm.registryhost.com//npm.registryhost.com/:_authToken=VALID_AUTH_TOKEN

After configuring your .npmrc, you can import the private package in your function code:

1
import package from 'npm:@myorg/private-package@v1.0.1'

Using a custom NPM registry

Some organizations require a custom NPM registry for security and compliance purposes. In such cases, you can specify the custom NPM registry to use via NPM_CONFIG_REGISTRY environment variable.

You can define it in the project's .env file or directly specify it when running the deploy command:

1
NPM_CONFIG_REGISTRY=https://custom-registry/ supabase functions deploy my-function

Importing types

If your environment is set up properly and the module you're importing is exporting types, the import will have types and autocompletion support.

Some npm packages may not ship out of the box types and you may need to import them from a separate package. You can specify their types with a @deno-types directive:

1
2
// @deno-types="npm:@types/express@^4.17"import express from 'npm:express@^4.17'

To include types for built-in Node APIs, add the following line to the top of your imports:

1
/// <reference types="npm:@types/node" />