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 usageQuick Reference
| Command | Description |
|---|---|
| deno run your_script.ts | Run a Deno script |
| deno run --allow-net your_script.ts | Run script with network access |
| deno run --allow-read your_script.ts | Run script with file read access |
| deno run --allow-env your_script.ts | Run script with environment variable access |
| deno run --unstable your_script.ts | Run 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).