Edge Functions

Managing dependencies

Managing packages and dependencies.


Developing with Edge Functions is similar to developing with Node.js, but with a few key differences. This guide will help you understand how to manage your dependencies.

Managing dependencies

There are two ways to manage your dependencies in Supabase Edge Functions:

Each function can have its own deno.json or deno.jsonc file to manage dependencies and configure Deno-specific settings. For a complete list of supported options, see the official Deno configuration documentation.

supabase/functions/my-function/deno.json

_10
{
_10
"imports": {
_10
"lodash": "https://cdn.skypack.dev/lodash"
_10
}
_10
}

This allows you to use simplified imports:


_10
import lodash from 'lodash'

The recommended file structure when using deno.json:


_10
└── supabase
_10
├── functions
_10
│ ├── function-one
_10
│ │ ├── index.ts
_10
│ │ └── deno.json # Function-specific Deno configuration
_10
│ └── function-two
_10
│ ├── index.ts
_10
│ └── deno.json # Function-specific Deno configuration
_10
└── 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.

supabase/functions/import_map.json

_10
{
_10
"imports": {
_10
"lodash": "https://cdn.skypack.dev/lodash"
_10
}
_10
}

The import map should be placed in the /supabase/functions folder and will be applied to all functions:


_10
└── supabase
_10
├── functions
_10
│ ├── import_map.json # Top-level import map for all functions
_10
│ ├── function-one
_10
│ │ └── index.ts
_10
│ └── function-two
_10
│ └── index.ts
_10
└── config.toml

You can override the import map location using the --import-map <string> flag with serve and deploy commands.

If using import maps with VSCode, update your .vscode/settings.json:

settings.json

_10
{
_10
"deno.enable": true,
_10
"deno.unstable": [
_10
"bare-node-builtins",
_10
"byonm"
_10
// ... other flags ...
_10
],
_10
"deno.importMap": "./supabase/functions/import_map.json"
_10
}

You can override the default import map location in two ways:

  1. Using the --import-map <string> flag with serve and deploy commands
  2. Setting the import_map property in your config.toml file for specific functions:
supabase/config.toml

_10
[functions.my-function]
_10
import_map = "./supabase/functions/my-function/custom_import_map.json"

For more configuration options, see the CLI Configuration documentation.

Importing dependencies

Supabase Edge Functions support several ways to import dependencies:

NPM Modules

You can import npm modules using the npm: specifier:


_10
import { createClient } from 'npm:@supabase/supabase-js@2'

Node.js Built-ins

For Node.js built-in APIs, use the node: specifier:


_10
import process from 'node:process'

Learn more about npm specifiers and Node built-in APIs in Deno's documentation.

Importing from Private Registries

Create a .npmrc file within supabase/functions. This will allow you to import the private packages into multiple functions. Alternatively, you can place the .npmrc file directly inside supabase/functions/function-name directory.)

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


_10
@myorg:registry=https://npm.registryhost.com
_10
//npm.registryhost.com/:_authToken=VALID_AUTH_TOKEN

After that, you can import the package directly in your function code or add it to the import_map.json (/docs/guides/functions/import-maps#using-import-maps).


_10
import MyPackage from 'npm:@myorg/private-package@v1.0.1'
_10
_10
// use MyPackage

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:


_10
// @deno-types="npm:@types/express@^4.17"
_10
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:


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