Deno restricts file access by default to keep your computer safe. You use --allow-read and --allow-write to let your program read or write files.
File system permissions (--allow-read, --allow-write) in Deno
deno run --allow-read=path/to/file_or_dir --allow-write=path/to/file_or_dir your_script.ts
You can specify exact files or directories after --allow-read or --allow-write.
Omitting the path after the flag allows access to all files for that permission.
data folder.deno run --allow-read=./data config_reader.ts
logs folder.deno run --allow-write=./logs logger.ts
deno run --allow-read --allow-write app.ts
This program writes a message to example.txt and then reads it back to print on the console.
You must run it with --allow-read=./example.txt and --allow-write=./example.txt to allow file access.
import { readTextFile, writeTextFile } from "https://deno.land/std@0.203.0/fs/mod.ts"; async function main() { const filePath = "./example.txt"; // Write text to a file await writeTextFile(filePath, "Hello from Deno!\n"); // Read the text back const content = await readTextFile(filePath); console.log(content); } main();
Always limit permissions to only what your program needs to keep your system safe.
If you forget to add these flags, Deno will show an error when trying to access files.
You can combine multiple paths separated by commas, like --allow-read=./data,./config.
--allow-read lets your program read files or folders you specify.
--allow-write lets your program create or change files or folders you specify.
Use these flags carefully to keep your computer safe while running Deno programs.
Practice
--allow-read flag do when running a Deno program?Solution
Step 1: Understand the purpose of
This flag grants permission to read files or folders.--allow-readStep 2: Compare with other flags
--allow-writeis for writing, not reading. Network access is unrelated.Final Answer:
Allows the program to read files or folders specified. -> Option AQuick Check:
--allow-read= read permission [OK]
- Confusing read permission with write permission
- Thinking it controls network access
- Assuming it grants all permissions
app.ts with permission to write files?Solution
Step 1: Recall Deno command syntax
The flags come immediately afterdeno runand before the script name.Step 2: Identify correct flag placement
deno run --allow-write app.tsis correct; options A and C have wrong order, D uses wrong flag.Final Answer:
deno run --allow-write app.ts -> Option DQuick Check:
Flags go afterdeno run[OK]
- Placing flags after the script name
- Mixing up --allow-read and --allow-write
- Putting flags before 'run'
readfile.ts that reads data.txt and prints its content. What happens if you run deno run readfile.ts without any flags?Solution
Step 1: Understand default permissions
Without--allow-read, Deno denies file reading for safety.Step 2: Predict script behavior
Trying to readdata.txtwithout permission causes a runtime error.Final Answer:
The script throws a permission error. -> Option CQuick Check:
No--allow-readmeans no file reading [OK]
- Assuming Deno allows reading by default
- Thinking the file is created automatically
- Believing the script prints empty content silently
log.txt. Running deno run --allow-read write.ts causes a permission error when writing. What is the fix?Solution
Step 1: Identify the permission needed
Writing files requires--allow-write, not just--allow-read.Step 2: Apply correct flag
Run the script with--allow-writeto allow file writing.Final Answer:
Add--allow-writeflag when running the script. -> Option AQuick Check:
Write permission needs--allow-write[OK]
- Using only --allow-read for writing
- Removing flags thinking it grants all permissions
- Confusing network permission with file permission
input.txt and writes to output.txt. Which command correctly grants the minimum permissions needed?Solution
Step 1: Understand fine-grained permissions
Using--allow-read=pathand--allow-write=pathlimits access to specific files.Step 2: Match permissions to files
Read permission forinput.txtand write permission foroutput.txtis needed.Step 3: Check options
deno run --allow-read=./input.txt --allow-write=./output.txt script.ts correctly assigns read to input and write to output. deno run --allow-read --allow-write script.ts grants full access, which is more than needed. deno run --allow-write=./input.txt --allow-read=./output.txt script.ts swaps permissions. deno run --allow-net script.ts is unrelated.Final Answer:
deno run --allow-read=./input.txt --allow-write=./output.txt script.ts -> Option BQuick Check:
Grant only needed file permissions [OK]
- Granting full read/write when only specific files needed
- Swapping read and write paths
- Using unrelated flags like --allow-net
Ready to prove it?
Pass the test and this pattern moves unknown
