0
0
DenoHow-ToBeginner ยท 3 min read

How to Use --allow-net in Deno for Network Access

Use the --allow-net flag when running a Deno script to grant it permission to access the network. You can specify it simply as deno run --allow-net script.ts to allow all network access or restrict it to specific domains like --allow-net=example.com.
๐Ÿ“

Syntax

The --allow-net flag controls network access permissions for your Deno scripts. You can use it in two ways:

  • --allow-net: Allows all network access.
  • --allow-net=domain1,domain2: Allows network access only to specified domains.

This flag must be passed when running your script with deno run or other Deno commands that execute code.

bash
deno run --allow-net script.ts

deno run --allow-net=example.com,api.example.com script.ts
๐Ÿ’ป

Example

This example shows a simple Deno script that fetches data from a website. The script requires network permission granted by --allow-net to run successfully.

typescript
const response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
const data = await response.json();
console.log(data);
Output
{ userId: 1, id: 1, title: "delectus aut autem", completed: false }
โš ๏ธ

Common Pitfalls

Common mistakes when using --allow-net include:

  • Not providing the flag at all, causing a permission error.
  • Specifying incorrect or incomplete domain names when restricting access.
  • Expecting network access without the flag, which Deno blocks by default for security.

Always check error messages for permission denials and add the flag accordingly.

bash
/* Wrong: Missing --allow-net flag */
deno run fetch_data.ts

/* Right: With --allow-net flag */
deno run --allow-net fetch_data.ts
๐Ÿ“Š

Quick Reference

Flag UsageDescription
--allow-netAllow all network access
--allow-net=example.comAllow network access only to example.com
--allow-net=example.com,api.comAllow network access to multiple specified domains
No --allow-netNo network access allowed (default)
โœ…

Key Takeaways

Use --allow-net to grant network access permission to your Deno scripts.
You can restrict network access to specific domains by listing them after --allow-net.
Deno blocks network access by default for security, so forgetting this flag causes errors.
Check error messages to identify missing network permissions quickly.
Always specify only the domains your script needs to minimize security risks.