0
0
DenoHow-ToBeginner ยท 3 min read

How to Use --allow-write Flag in Deno for File Write Permissions

Use the --allow-write flag when running a Deno script to grant it permission to write files or directories on your system. For example, run deno run --allow-write script.ts to allow file writing. Without this flag, Deno will block any write operations for security.
๐Ÿ“

Syntax

The --allow-write flag is used with the deno run command to allow your script to write files or directories.

You can specify it in these ways:

  • --allow-write: Allows writing anywhere on the file system.
  • --allow-write=path: Allows writing only inside the specified path.
bash
deno run --allow-write script.ts
deno run --allow-write=./data script.ts
๐Ÿ’ป

Example

This example shows a script that writes a text file. Running it with --allow-write lets it create the file. Without the flag, Deno will block the write and show an error.

typescript
const encoder = new TextEncoder();
const data = encoder.encode("Hello from Deno!\n");

await Deno.writeFile("hello.txt", data);
console.log("File written successfully.");
Output
File written successfully.
โš ๏ธ

Common Pitfalls

Common mistakes when using --allow-write include:

  • Not adding the flag, causing permission errors when writing files.
  • Using --allow-write=path but specifying the wrong or incomplete path, which blocks writes outside that path.
  • Assuming --allow-write also grants read permission; you must add --allow-read separately if needed.
bash
/* Wrong: Missing --allow-write flag */
deno run script.ts

/* Right: Adding --allow-write */
deno run --allow-write script.ts
๐Ÿ“Š

Quick Reference

Flag UsageDescription
--allow-writeAllow writing files anywhere
--allow-write=pathAllow writing only inside specified path
--allow-readAllow reading files (needed separately)
No flagNo write permission, write operations fail
โœ…

Key Takeaways

Always use --allow-write to enable file writing in Deno scripts.
Specify a path with --allow-write=path to limit write access for safety.
Without --allow-write, Deno blocks all write operations by default.
Combine --allow-write with --allow-read if your script reads and writes files.
Check paths carefully when restricting write permissions to avoid errors.