-
Notifications
You must be signed in to change notification settings - Fork 280
/
Copy pathenvironment_variables.ts
41 lines (35 loc) · 1.66 KB
/
environment_variables.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* @title Environment variables
* @difficulty beginner
* @tags cli, deploy
* @run -E <url>
* @resource {https://docs.deno.com/api/deno/~/Deno.env} Doc: Deno.env
* @resource {https://docs.deno.com/deploy/manual/environment-variables} Deploy Docs: Environment Variables
* @group System
*
* Environment variables can be used to configure the behavior of a program,
* or pass data from one program to another.
*/
// Here an environment variable with the name "PORT" is read. If this variable
// is set the return value will be a string. If it is unset it will be `undefined`.
const PORT = Deno.env.get("PORT");
console.log("PORT:", PORT);
// You can also get an object containing all environment variables.
const env = Deno.env.toObject();
console.log("env:", env);
// Environment variables can also be set. The set environment variable only affects
// the current process, and any new processes that are spawned from it. It does
// not affect parent processes or the user shell.
Deno.env.set("MY_PASSWORD", "123456");
// You can also unset an environment variable.
Deno.env.delete("MY_PASSWORD");
// Note that environment variables are case-sensitive on unix, but not on
// Windows. This means that these two invocations will have different results
// between platforms.
Deno.env.set("MY_PASSWORD", "123");
Deno.env.set("my_password", "456");
console.log("UPPERCASE:", Deno.env.get("MY_PASSWORD"));
console.log("lowercase:", Deno.env.get("my_password"));
// Access to environment variables is only possible if the Deno process is
// running with env var permissions (`-E`). You can limit the permission
// to only a specific number of environment variables (`-E=PORT,MY_PASSWORD`).