Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore(docs): add specification about recommended functions isolation (#31264)

Andrew Valleteau85f75141276a
diff --git a/apps/docs/content/guides/functions/import-maps.mdx b/apps/docs/content/guides/functions/import-maps.mdx
index 176aa73d4f2..ddbdc7e87d5 100644
--- a/apps/docs/content/guides/functions/import-maps.mdx
+++ b/apps/docs/content/guides/functions/import-maps.mdx
@@ -6,13 +6,22 @@ subtitle: 'Managing packages and dependencies.'
tocVideo: 'ILr3cneZuFk'
---
-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.
+Developing with Edge Functions is similar to developing with Node.js, but with a few key differences.
+
+In the Deno ecosystem, each function should be treated as an independent project with its own set of dependencies and configurations. This "isolation by design" approach:
+
+- Ensures each function has explicit control over its dependencies
+- Prevents unintended side effects between functions
+- Makes deployments more predictable and maintainable
+- Allows for different versions of the same dependency across functions
+
+For these reasons, we recommend maintaining separate configuration files (`deno.json`/`deno.jsonc`, `.npmrc`, or `import_map.json`) within each function's directory, even if it means duplicating some configurations.
## Managing dependencies
There are two ways to manage your dependencies in Supabase Edge Functions:
-### Using deno.json (recommended)
+### Using deno.jsonc (recommended)
<Admonition type="info">
@@ -20,7 +29,7 @@ This feature requires Supabase CLI version 1.215.0 or higher.
</Admonition>
-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](https://docs.deno.com/runtime/manual/getting_started/configuration_file).
+Each function should have its own `deno.jsonc` or `deno.json` file to manage dependencies and configure Deno-specific settings. This ensures proper isolation between functions and is the recommended approach for deployment. For a complete list of supported options, see the [official Deno configuration documentation](https://docs.deno.com/runtime/manual/getting_started/configuration_file).
```json supabase/functions/my-function/deno.json
{
@@ -30,31 +39,35 @@ Each function can have its own `deno.json` or `deno.jsonc` file to manage depend
}
```
-This allows you to use simplified imports:
-
-```ts
-import lodash from 'lodash'
-```
-
-The recommended file structure when using `deno.json`:
+The recommended file structure for deployment:
```bash
└── supabase
├── functions
│ ├── function-one
│ │ ├── index.ts
- │ │ └── deno.json # Function-specific Deno configuration
+ │ │ ├─- deno.jsonc # Function-specific Deno configuration
+ │ │ └── .npmrc # Function-specific npm configuration (if needed)
│ └── function-two
│ ├── index.ts
- │ └── deno.json # Function-specific Deno configuration
+ │ ├─- deno.jsonc # Function-specific Deno configuration
+ │ └── .npmrc # Function-specific npm configuration (if needed)
└── config.toml
```
+<Admonition type="caution">
+ While it's possible to use a global `deno.json` or `deno.jsonc` in the `/supabase/functions`
+ directory for local development, this approach is not recommended for deployment. Each function
+ should maintain its own configuration to ensure proper isolation and dependency management.
+</Admonition>
+
### 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.
+Import Maps are a legacy way to manage dependencies, similar to a `package.json` file. While still supported, we recommend using `deno.jsonc`. If both exist, `deno.jsonc` takes precedence.
-```json supabase/functions/import_map.json
+Each function should have its own `import_map.json` file for proper isolation:
+
+```json supabase/functions/my-function/import_map.json
{
"imports": {
"lodash": "https://cdn.skypack.dev/lodash"
@@ -62,22 +75,27 @@ Import Maps are a legacy way to manage dependencies, similar to a `package.json`
}
```
-The import map should be placed in the `/supabase/functions` folder and will be applied to all functions:
+The recommended file structure:
```bash
└── supabase
├── functions
- │ ├── import_map.json # Top-level import map for all functions
│ ├── function-one
- │ │ └── index.ts
+ │ │ ├── index.ts
+ │ │ └── import_map.json # Function-specific import map
│ └── function-two
- │ └── index.ts
+ │ ├── index.ts
+ │ └── import_map.json # Function-specific import map
└── config.toml
```
-You can override the import map location using the `--import-map <string>` flag with `serve` and `deploy` commands.
+<Admonition type="caution">
+ While it's possible to use a global `import_map.json` in the `/supabase/functions` directory for
+ local development, this approach is not recommended for deployment. Each function should maintain
+ its own import map to ensure proper isolation.
+</Admonition>
-If using import maps with VSCode, update your `.vscode/settings.json`:
+If using import maps with VSCode, update your `.vscode/settings.json` to point to your function-specific import map:
```json settings.json
{
@@ -87,21 +105,52 @@ If using import maps with VSCode, update your `.vscode/settings.json`:
"byonm"
// ... other flags ...
],
- "deno.importMap": "./supabase/functions/import_map.json"
+ "deno.importMap": "./supabase/functions/my-function/import_map.json"
}
```
-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:
+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:
```toml supabase/config.toml
[functions.my-function]
-import_map = "./supabase/functions/my-function/custom_import_map.json"
+import_map = "./supabase/functions/my-function/import_map.json"
+```
+
+### Importing from private registries
+
+<Admonition type="info">This feature requires Supabase CLI version 1.207.9 or higher.</Admonition>
+
+To use private npm packages, create a `.npmrc` file within your function directory. This ensures proper isolation and dependency management for each function.
+
+```bash
+└── supabase
+ └── functions
+ └── my-function
+ ├── index.ts
+ ├── deno.jsonc
+ └── .npmrc # Function-specific npm configuration
+```
+
+Add your registry details in the `.npmrc` file. Follow [this guide](https://docs.npmjs.com/cli/v10/configuring-npm/npmrc) to learn more about the syntax of npmrc files.
+
+```plaintext
+@myorg:registry=https://npm.registryhost.com
+//npm.registryhost.com/:_authToken=VALID_AUTH_TOKEN
```
-For more configuration options, see the [CLI Configuration documentation](/docs/guides/local-development/cli/config#functions.function_name.import_map).
+<Admonition type="caution">
+ While it's possible to use a global `.npmrc` in the `/supabase/functions` directory for local
+ development, we recommend using function-specific `.npmrc` files for deployment to maintain proper
+ isolation.
+</Admonition>
+
+After configuring your `.npmrc`, you can import the private package in your function code:
+
+```ts
+import MyPackage from 'npm:@myorg/[email protected]'
+
+// use MyPackage
+```
## Importing dependencies