Run TypeScript Directly in Deno: A Practical Guide to Native Execution
Deno executes TypeScript files natively without a separate compile step. This guide covers running .ts files directly, adding permissions, enabling static checks with --check, importing remote modules, and handling startup latency and cache issues.
27 Aug 2026, 15:36 UTC

Desired Outcome
Execute TypeScript files directly with deno run without a separate compilation step, while retaining type safety through optional static checking and respecting Deno's permission model.
Prerequisites
- Deno 1.0 or later installed (
deno --versionshould report 1.0.0 or higher) - Basic familiarity with TypeScript syntax and ES modules
- A terminal with permission to execute Deno commands
Procedure
1. Create a TypeScript Entry Point
Write a .ts file using standard ES module imports. Deno resolves dependencies from URLs or local paths without package.json or node_modules.
// hello.ts
export function greet(name: string): string {
return `Hello, ${name}!`;
}
if (import.meta.main) {
console.log(greet("Deno"));
}
2. Run the File Directly
Execute the script with deno run. Deno compiles TypeScript on-the-fly using its built-in compiler (based on swc).
deno run hello.ts
Expected output:
Hello, Deno!
3. Add Runtime Permissions
Deno's security model requires explicit flags for I/O, network, and environment access. TypeScript execution does not change this.
// read-file.ts
const decoder = new TextDecoder();
const data = await Deno.readFile("data.txt");
console.log(decoder.decode(data));
deno run --allow-read read-file.ts
Omitting --allow-read produces a PermissionDenied error at runtime, not compile time.
4. Enable Static Type Checking Before Execution
Use --check to run the type checker without executing code. This catches type errors early in CI or pre-commit hooks.
deno check hello.ts
# or
deno run --check hello.ts
deno check exits with a non-zero code if type errors exist. deno run --check type-checks then runs if clean.
5. Import External TypeScript Modules
Deno imports TypeScript directly from URLs. The compiler resolves and type-checks remote modules on first run, caching them locally.
// fetch-example.ts
import { parse } from "https://deno.land/std@0.224.0/flags/mod.ts";
const args = parse(Deno.args, { string: ["name"] });
console.log(greet(args.name ?? "world"));
deno run --allow-net fetch-example.ts --name=Reader
Expected Checks
| Check | Command | Success Indicator |
|---|---|---|
| Basic execution | deno run hello.ts | Prints output without tsc invocation |
| Type checking | deno check hello.ts | Exit code 0, no diagnostics |
| Permission handling | deno run read-file.ts (no flags) | PermissionDenied for read |
| JS compatibility | deno run hello.js (renamed) | Identical runtime behavior |
Recovery Options
Type Errors at Runtime
If deno run fails with a type error (rare, since Deno checks before execution), fix the source and re-run. Use deno check first to isolate type issues from runtime logic.
Permission Denied
Add the minimal required flag: --allow-read, --allow-write, --allow-net, --allow-env, --allow-run. Avoid -A (allow all) in production.
Slow Startup on Large Projects
Runtime compilation adds latency proportional to module graph size. Mitigations:
- Use
deno compileto produce a standalone binary for distribution - Leverage Deno's cache: subsequent runs reuse compiled output unless sources change
- Split large entry points; Deno caches per-module
Cache Corruption
If imports behave unexpectedly, clear the compilation cache:
deno cache --reload fetch-example.ts
This forces re-fetch and re-compilation of all dependencies.
Limitations
- Startup overhead: First run compiles the full module graph. Cold starts are slower than pre-bundled JavaScript.
- No
tsconfig.jsonrequired: Deno uses sensible defaults (strict mode, ESNext target). Custom compiler options are limited tocompilerOptionsindeno.jsonand only affectdeno check/deno lint, not runtime compilation. - Type-only imports: Use
import typefor types erased at runtime; Deno respects this but does not enforce it beyond standard TS behavior.
Verification Checklist
- Confirm Deno version:
deno --version≥ 1.0.0 - Run a minimal
.tsfile withouttscinstalled - Verify
--checkcatches a deliberate type error - Confirm permission flags gate I/O independently of TypeScript
- Compare
deno run example.tsanddeno run example.jsfor identical output
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.