0
0
DenoHow-ToBeginner ยท 3 min read

How to Use --allow-read Permission Flag in Deno

Use the --allow-read flag when running a Deno script to grant it permission to read files or directories from the file system. For example, run deno run --allow-read script.ts to allow all read access, or specify a path like --allow-read=./data to restrict access to a specific folder.
๐Ÿ“

Syntax

The --allow-read flag controls file system read permissions for your Deno script. You can use it in two ways:

  • --allow-read: Grants read access to all files and directories.
  • --allow-read=PATH: Grants read access only to the specified file or directory path.

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

bash
deno run --allow-read script.ts

deno run --allow-read=./folder script.ts
๐Ÿ’ป

Example

This example shows a Deno script that reads and prints the content of a file named hello.txt. You must run it with --allow-read to allow the script to access the file.

typescript
import { readTextFile } from "https://deno.land/std@0.203.0/fs/mod.ts";

async function main() {
  try {
    const content = await Deno.readTextFile("hello.txt");
    console.log("File content:", content);
  } catch (error) {
    console.error("Error reading file:", error.message);
  }
}

main();
Output
File content: Hello, Deno!
โš ๏ธ

Common Pitfalls

Common mistakes when using --allow-read include:

  • Not providing the flag at all, causing a permission error.
  • Using --allow-read without specifying a path but expecting restricted access.
  • Specifying an incorrect or non-existent path, which denies access.

Always check the exact path you want to allow and ensure the flag is included when running your script.

bash
Wrong (missing flag):

deno run script.ts

Right (allow all reads):

deno run --allow-read script.ts

Right (allow specific folder):

deno run --allow-read=./data script.ts
๐Ÿ“Š

Quick Reference

Summary of --allow-read usage:

Flag UsageDescription
--allow-readGrants read access to all files and directories.
--allow-read=PATHGrants read access only to the specified file or directory path.
No flagNo read access; script will throw permission errors if reading files.
โœ…

Key Takeaways

Always use --allow-read to grant your Deno script permission to read files.
Specify a path with --allow-read=PATH to limit read access to certain files or folders.
Omitting --allow-read causes permission errors when reading files.
Check your file paths carefully to avoid access denial.
Use the flag when running scripts, not inside the code.