管理依赖
Handle dependencies within Edge Functions.
导入依赖 #
🌐 Importing dependencies
Supabase Edge Functions 支持多种方式来导入依赖:
🌐 Supabase Edge Functions support several ways to import dependencies:
- 来自 npm 的 JavaScript 模块 (https://docs.deno.com/examples/npm/)
- 内置 Node API
- 发布到 JSR 或 deno.land/x 的模块
1// NPM packages (recommended)2import { createClient } from 'npm:@supabase/supabase-js@2'34// Node.js built-ins5import process from 'node:process'67// JSR modules (Deno's registry)8import path from 'jsr:@std/path@1.0.8'使用 deno.json#
🌐 Using deno.json (recommended)
每个函数都应该有自己的 deno.json 文件来管理依赖和配置 Deno 特定的设置。这可以确保函数之间的隔离,也是部署的推荐做法。当你更新某个函数的依赖时,不会意外地破坏需要不同版本的其他函数。
🌐 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 "imports": {3 "supabase": "npm:@supabase/supabase-js@2",4 "lodash": "https://cdn.skypack.dev/lodash"5 }6}你可以直接把这个文件添加到函数自己的目录里:
🌐 You can add this file directly to the function’s own directory:
1└── supabase2 ├── functions3 │ ├── function-one4 │ │ ├── index.ts5 │ │ └── deno.json # Function-specific Deno configuration6 │ └── function-two7 │ ├── index.ts8 │ └── deno.json # Function-specific Deno configuration9 └── config.toml在 /supabase/functions 目录中可以使用全局的 deno.json 来进行本地开发,但这种方法不建议用于部署。每个函数都应该维护自己的配置,以确保适当的隔离和依赖管理。
🌐 It's possible to use a global deno.json in the /supabase/functions directory for local development, but this approach is not recommended for deployment. Each function should maintain its own configuration to ensure proper isolation and dependency management.
使用导入映射(旧版) #
🌐 Using import maps (legacy)
导入映射是一种管理依赖的传统方法,类似于 package.json 文件。虽然仍然支持,但我们建议使用 deno.json。如果两者都存在,deno.json 优先。
🌐 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_map.json 文件来实现适当的隔离:
🌐 Each function should have its own import_map.json file for proper isolation:
1# /function-one/import_map.json2{3 "imports": {4 "lodash": "https://cdn.skypack.dev/lodash"5 }6}这个 JSON 文件应该放在函数自己的目录里:
🌐 This JSON file should be located within the function’s own directory:
1└── supabase2 ├── functions3 │ ├── function-one4 │ │ ├── index.ts5 │ │ └── import_map.json # Function-specific import map在 /supabase/functions 目录中可以使用全局的 import_map.json 来进行本地开发,但这种方法不建议用于部署。每个函数都应该维护自己的配置,以确保适当的隔离和依赖管理。
🌐 It's possible to use a global import_map.json in the /supabase/functions directory for local development, but this approach is not recommended for deployment. Each function should maintain its own configuration to ensure proper isolation and dependency management.
如果你在 VSCode 中使用导入映射,请更新你的 .vscode/settings.json,指向你的函数专用导入映射:
🌐 If you’re using import maps with VSCode, update your .vscode/settings.json to point to your function-specific import map:
1{2 "deno.enable": true,3 "deno.unstable": ["bare-node-builtins", "byonm"],4 "deno.importMap": "./supabase/functions/function-one/import_map.json"5}你可以使用 --import-map <string> 标志在 serve 和 deploy 命令中覆盖默认的导入映射位置,或者在你的 config.toml 文件中设置 import_map 属性:
🌐 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[functions.my-function]2import_map = "./supabase/functions/function-one/import_map.json"私有 NPM 包 #
🌐 Private NPM packages
要使用私有 npm 包,在你的函数目录里创建一个 .npmrc 文件。
🌐 To use private npm packages, create a .npmrc file within your function’s own directory.
This feature requires Supabase CLI version 1.207.9 or higher.
1└── supabase2 └── functions3 └── my-function4 ├── index.ts5 ├── deno.json6 └── .npmrc # Function-specific npm configuration在 /supabase/functions 目录中可以使用全局的 .npmrc 来进行本地开发,但这种方法不建议用于部署。每个函数都应该维护自己的配置,以确保适当的隔离和依赖管理。
🌐 It's possible to use a global .npmrc in the /supabase/functions directory for local development, but this approach is not recommended for deployment. Each function should maintain its own configuration to ensure proper isolation and dependency management.
在 .npmrc 文件中添加你的注册表详细信息。查看 这个指南 了解更多关于 npmrc 文件语法的内容。
🌐 Add your registry details in the .npmrc file. Follow this guide to learn more about the syntax of npmrc files.
1# /my-function/.npmrc2@myorg:registry=https://npm.registryhost.com3//npm.registryhost.com/:_authToken=VALID_AUTH_TOKEN在配置好你的 .npmrc 后,你就可以在函数代码中导入私有包了:
🌐 After configuring your .npmrc, you can import the private package in your function code:
1import package from 'npm:@myorg/private-package@v1.0.1'使用自定义 NPM 仓库 #
🌐 Using a custom NPM registry
This feature requires Supabase CLI version 2.2.8 or higher.
有些组织出于安全和合规的考虑需要使用自定义的 NPM 注册表。在这种情况下,你可以通过 NPM_CONFIG_REGISTRY 环境变量指定要使用的自定义 NPM 注册表。
🌐 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.
你可以在项目的 .env 文件中定义它,或者在运行部署命令时直接指定它:
🌐 You can define it in the project's .env file or directly specify it when running the deploy command:
1NPM_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.
有些 npm 包默认可能不会附带类型,你可能需要从一个单独的包中导入它们。你可以用 @deno-types 指令来指定它们的类型:
🌐 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// @deno-types="npm:@types/express@^4.17"2import express from 'npm:express@^4.17'要为内置的 Node API 添加类型,请在导入的顶部加上以下这一行:
🌐 To include types for built-in Node APIs, add the following line to the top of your imports:
1/// <reference types="npm:@types/node" />