First Steps
Now that you’ve installed Deno and completed the quickstart , let’s explore Deno’s powerful built-in tools and common commands.
The Deno Command
The deno executable includes everything you need for JavaScript and TypeScript development. Let’s explore its capabilities.
Getting Help
View all available commands:
Get help for a specific command:
deno run --help
deno test --help
Check Version
See your Deno version and component versions:
Example output:
deno 1.40.0 (release, x86_64-apple-darwin)
v8 11.9.169.7
typescript 5.3.3
Core Commands
Let’s explore the essential commands you’ll use daily.
Running Code
The run command executes JavaScript or TypeScript files:
Basic
With Permissions
From URL
Watch Mode
Interactive REPL
Start an interactive Read-Eval-Print Loop:
Try it out:
Deno 1.40 . 0
exit using ctrl +d, ctrl +c, or close()
> const greeting = "Hello, Deno!"
> console . log ( greeting )
Hello , Deno !
> Deno . version
{ deno : "1.40.0" , v8 : "11.9.169.7" , typescript : "5.3.3" }
The REPL is great for quick experiments and testing snippets!
Deno includes an opinionated code formatter:
# Format files in place
deno fmt
# Check if files are formatted
deno fmt --check
# Format specific files
deno fmt script.ts utils.ts
The formatter supports JavaScript, TypeScript, JSON, and Markdown.
Linting Code
Catch common errors and enforce best practices:
# Lint current directory
deno lint
# Lint specific files
deno lint src/
# Show available rules
deno lint --rules
Example output:
error[no-unused-vars]: `x` is never used
--> script.ts:1:7
|
1 | const x = 5;
| ^
= hint: Remove unused variable or prefix it with underscore
Running Tests
Deno has a built-in test runner:
Create a test file
Create a file ending in _test.ts or .test.ts: import { assertEquals } from "https://deno.land/std/assert/mod.ts" ;
import { add } from "./add.ts" ;
Deno . test ( "add function" , () => {
assertEquals ( add ( 2 , 3 ), 5 );
assertEquals ( add ( - 1 , 1 ), 0 );
});
Run your tests
Output: running 1 test from ./add_test.ts
add function ... ok (2ms)
ok | 1 passed | 0 failed (10ms)
Test command variations:
# Run tests with coverage
deno test --coverage
# Run specific test file
deno test add_test.ts
# Filter tests by name
deno test --filter "add"
# Watch mode for tests
deno test --watch
Type Checking
Explicitly type-check your code without running it:
This is useful for:
CI/CD pipelines
Pre-commit hooks
Large codebases where you want to check types without execution
Working with Dependencies
Import Maps
Deno supports import maps for managing dependencies:
Use it:
deno run --import-map=import_map.json main.ts
In your code:
import { serve } from "std/http/server.ts" ;
import { Application } from "oak" ;
Caching Dependencies
Deno caches remote dependencies automatically. To explicitly cache:
Reload dependencies:
deno cache --reload script.ts
Clear the cache:
rm -rf $( deno info --json | jq -r .denoDir )
Info Command
View information about a module and its dependencies:
Example output:
local: /path/to/script.ts
type: TypeScript
dependencies: 3 unique
size: 15.2KB
file:///path/to/script.ts (1.5KB)
├── https://deno.land/std/http/server.ts (8.3KB)
└── https://deno.land/std/path/mod.ts (5.4KB)
Built-in Utilities
Generating Documentation
Generate documentation from your code comments:
# View docs in terminal
deno doc script.ts
# Generate HTML documentation
deno doc --html --name= "My Project" src/
Document your code with JSDoc:
/**
* Adds two numbers together.
* @param a - First number
* @param b - Second number
* @returns The sum of a and b
*/
export function add ( a : number , b : number ) : number {
return a + b ;
}
Benchmarking
Measure performance with built-in benchmarks:
Deno . bench ( "string concatenation" , () => {
let str = "" ;
for ( let i = 0 ; i < 1000 ; i ++ ) {
str += "a" ;
}
});
Deno . bench ( "array join" , () => {
const arr = [];
for ( let i = 0 ; i < 1000 ; i ++ ) {
arr . push ( "a" );
}
arr . join ( "" );
});
Run benchmarks:
Compiling to Executable
Compile your Deno program to a standalone executable:
deno compile --allow-net --output server server.ts
This creates a binary that can run without Deno installed:
Compiled executables include the Deno runtime, so they’re self-contained but larger in size.
Permission Management
Understanding Deno’s permission system is crucial for security.
Available Permissions
Flag Description --allow-all or -AAllow all permissions --allow-envAllow environment access --allow-netAllow network access --allow-readAllow file system read access --allow-writeAllow file system write access --allow-runAllow running subprocesses --allow-ffiAllow loading dynamic libraries --allow-hrtimeAllow high-resolution time measurement
Granular Permissions
You can grant permissions for specific resources:
# Allow reading only from specific directory
deno run --allow-read=/home/user/data script.ts
# Allow network access to specific domains
deno run --allow-net=api.example.com script.ts
# Allow running specific commands
deno run --allow-run=git script.ts
# Multiple specific permissions
deno run --allow-read=/data --allow-net=api.example.com script.ts
Permission Prompts
Without explicit permissions, Deno will prompt at runtime:
⚠️ ️Deno requests network access to "api.example.com".
Run again with --allow-net to bypass this prompt.
Allow? [y/n/A] (y = yes, allow; n = no, deny; A = allow all net permissions)
Use --allow-all or -A only during development. Always use specific permissions in production!
Configuration Files
Create a deno.json or deno.jsonc file to configure your project:
{
"tasks" : {
"dev" : "deno run --allow-net --watch server.ts" ,
"test" : "deno test --coverage" ,
"fmt" : "deno fmt" ,
"lint" : "deno lint"
},
"imports" : {
"std/" : "https://deno.land/[email protected] /"
},
"lint" : {
"rules" : {
"tags" : [ "recommended" ]
}
},
"fmt" : {
"indentWidth" : 2 ,
"lineWidth" : 80
}
}
Run tasks with:
deno task dev
deno task test
Environment Variables
Load environment variables from a .env file:
API_KEY = secret123
DATABASE_URL = postgres://localhost/mydb
Access in your code:
import { load } from "https://deno.land/std/dotenv/mod.ts" ;
await load ({ export: true });
const apiKey = Deno . env . get ( "API_KEY" );
console . log ( apiKey );
Run with:
deno run --allow-env --allow-read script.ts
Next Steps
You now have a solid foundation in Deno’s core functionality. Continue learning:
API Reference Explore all available Deno APIs and runtime features
Standard Library Discover utilities for common programming tasks
Deploy Your App Deploy your Deno applications to production
Examples Learn from real-world examples and patterns