设置和获取环境变量
Deno Sandboxes provide a sandboxed environment for evaluating JavaScript code. This is useful for evaluating code that is not trusted or for testing code that is not safe to run in the main runtime.
你可以使用 sandbox.env.set() 方法在沙箱中设置环境变量。
import { Sandbox } from "@deno/sandbox";
await using sandbox = await Sandbox.create();
// 设置环境变量
await sandbox.env.set("API_KEY", "secret-key-123");
await sandbox.env.set("NODE_ENV", "production");
// 在脚本中使用它们
const apiKey = await sandbox.sh`echo $API_KEY`.text();
console.log("API_KEY:", apiKey.trim());
For more information about Sandboxes, see the Sandboxes documentation.