0
0
DenoHow-ToBeginner ยท 3 min read

How to Run a Deno Script: Simple Steps and Example

To run a Deno script, use the command deno run your_script.ts in your terminal. This command executes the TypeScript or JavaScript file with Deno's runtime environment.
๐Ÿ“

Syntax

The basic syntax to run a Deno script is:

  • deno: The Deno runtime command.
  • run: The command telling Deno to execute a script.
  • your_script.ts: The path to your TypeScript or JavaScript file.

You can add flags like --allow-net to grant network access or --allow-read for file system access.

bash
deno run [options] your_script.ts
๐Ÿ’ป

Example

This example shows a simple Deno script that prints a greeting message to the console.

typescript
console.log("Hello from Deno!");
Output
Hello from Deno!
โš ๏ธ

Common Pitfalls

One common mistake is forgetting to grant permissions when your script needs them. Deno is secure by default and blocks access to files, network, or environment unless explicitly allowed.

Another mistake is running the script without the run command or using an unsupported file extension.

bash
deno your_script.ts  # โŒ Incorrect, missing 'run'
deno run your_script.ts  # โœ… Correct usage
๐Ÿ“Š

Quick Reference

CommandDescription
deno run your_script.tsRun a Deno script
deno run --allow-net your_script.tsRun script with network access
deno run --allow-read your_script.tsRun script with file read access
deno run --allow-env your_script.tsRun script with environment variable access
deno run --unstable your_script.tsRun script with unstable APIs enabled
โœ…

Key Takeaways

Use deno run your_script.ts to execute Deno scripts.
Add permission flags like --allow-net when your script needs access.
Deno is secure by default; always grant explicit permissions.
Always include the run command before the script file name.
Use TypeScript or JavaScript files with supported extensions (.ts, .js).