Deno’s command-line interface (CLI) is the core entry point for interaction, offering rich functionality and flexible configuration options. This article provides a comprehensive analysis of the Deno CLI, from basic commands to advanced usage, covering its working principles and practical techniques.
Deno CLI Core Architecture
CLI Command Structure
Deno CLI adopts a hierarchical command structure, categorized into three types:
- Core Commands: Provided directly by the Deno binary
run: Executes scriptsbundle: Bundles modulesfmt: Formats codetest: Runs testslint: Performs code linting
- Subcommands: Extensions of core commands
run --watch: File watching modetest --coverage: Coverage reporting
- Global Flags: Universal options across commands
--allow-net: Network permissions--import-map: Import map file
Detailed Execution Process:
- Argument Parsing: Uses the
claplibrary to parse command-line arguments - Permission Verification: Checks
--allow-*permission flags - Configuration Loading: Reads
deno.jsonandimport_map.json - Functionality Execution: Invokes corresponding Rust implementation
- Result Output: Formats output to the console
Deep Dive into Core Commands
deno run Command
Basic Usage:
deno run [OPTIONS] <SCRIPT_ARG>...Advanced Parameters Example:
# Run with permission control and cache optimization
deno run \
--allow-read=./data \
--allow-net=api.example.com \
--reload=https://deno.land/x/oak \
--v8-flags=--turbofan \
--import-map=import_map.json \
src/main.ts arg1 arg2Parameter Details Table:
| Parameter | Purpose | Example |
|---|---|---|
--allow-read | File read permission | --allow-read=./config |
--allow-write | File write permission | --allow-write=./logs |
--allow-net | Network access permission | --allow-net=api.example.com |
--reload | Force cache refresh | --reload=https://deno.land/x/* |
--v8-flags | V8 engine options | --v8-flags=--harmony |
--import-map | Import map file | --import-map=import_map.json |
--watch | File watching mode | --watch=src/**/*.ts |
deno bundle Command
Module Bundling Principle:
- Resolves dependency graph of the entry file
- Applies import map for path resolution
- Merges all dependencies into a single file
- Generates optimized ES module or IIFE format
Advanced Bundling Example:
# Generate browser-compatible UMD bundle
deno bundle \
--import-map=import_map.json \
--no-check \
src/main.ts \
dist/bundle.js
# Generate type declaration file
deno bundle --config tsconfig.json src/main.ts dist/bundle.d.tsFormat Selection Guide:
| Format | Use Case | Features |
|---|---|---|
| ES Module | Modern browsers/Node.js | Supports dynamic imports |
| IIFE | Legacy browsers | Immediately Invoked Function Expression |
| UMD | Universal modules | Compatible with multiple environments |
deno test Command
Test Execution Process:
- Discovers test files (
*_test.ts) - Loads test modules
- Executes test cases
- Generates test reports
Advanced Testing Features:
# Parallel testing with coverage
deno test \
--allow-read=./testdata \
--parallel \
--coverage=./coverage \
--filter="UserTest" \
tests/
# Generate multi-format reports
deno coverage ./coverage \
--lcov \
--output=lcov.info
genhtml lcov.info -o coverage_reportTest Filtering Options:
| Option | Purpose | Example |
|---|---|---|
--filter | Regex match test names | --filter="Login.*" |
--shuffle | Randomize execution order | --shuffle |
--fail-fast | Stop on first failure | --fail-fast |
Global Flags Explained
Permission Control Flags
Permission Matrix:
| Permission | Flag | Sub-Permissions |
|---|---|---|
| File System | --allow-read--allow-write | Specific paths |
| Network | --allow-net | Specific domains |
| Environment Variables | --allow-env | Specific variable names |
| Subprocess Execution | --allow-run | Restricted commands |
| Plugins | --allow-plugin | Rarely used |
Path Restriction Example:
# Precise path control
deno run \
--allow-read=/etc/config.json \
--allow-write=./logs/app.log \
app.ts
# Wildcard usage
deno run --allow-read=./data/*.json app.tsDevelopment Tool Flags
Common Development Flags:
| Flag | Purpose | Example |
|---|---|---|
--watch | File watching | --watch=src/**/*.ts |
--inspect | Debug mode | --inspect=9229 |
--no-check | Skip type checking | --no-check |
--location | Simulate URL | --location=http://localhost |
File Watching Mode:
# Watch multiple directories
deno run --watch=src/,tests/
# Customize watch interval
DENO_WATCH_POLL_INTERVAL_MS=1000 deno run --watch=src/Configuration File System
deno.json Configuration
Complete Configuration Example:
{
"tasks": {
"dev": "deno run --allow-net --watch src dev_server.ts",
"build": "deno compile --allow-net --import-map=import_map.json src/main.ts --output dist/main",
"test": "deno test --coverage=cov_profile"
},
"imports": {
"react": "https://esm.sh/react@18.3.1",
"lodash": "https://esm.sh/lodash@4.17.21"
},
"compilerOptions": {
"lib": ["dom", "esnext"],
"target": "es2022"
},
"lint": {
"rules": {
"tags": ["recommended"],
"include": ["src/**/*.ts"]
}
},
"fmt": {
"files": {
"include": ["src/**/*.ts"],
"exclude": ["src/**/*.test.ts"]
}
}
}Configuration Items Explained:
| Item | Purpose | Example |
|---|---|---|
tasks | Define common commands | "dev": "deno run..." |
imports | Specify import map file | "react": "https://esm.sh/..." |
compilerOptions | TypeScript compilation options | "target": "es2022" |
lint | ESLint configuration | "rules": {...} |
fmt | Prettier configuration | "files": {...} |
import_map.json Management
Version Control Strategy:
{
"imports": {
"react": "https://esm.sh/react@18.3.1",
"lodash": "https://esm.sh/lodash@4.17.21"
},
"scopes": {
"https://esm.sh/": {
"@types/react": "https://esm.sh/@types/react@18.3.3"
}
}
}Updating Dependency Versions:
# Manually update version
sed -i 's/react@18.3.1/react@18.3.2/g' import_map.json
# Use tool to auto-update
deno run --allow-read=import_map.json update_deps.tsAdvanced Usage Techniques
Custom Subcommands
Shell-Based Encapsulation:
#!/bin/bash
# deno-mycommand
case $1 in
"start")
deno run --allow-net server.ts
;;
"build")
deno compile --allow-net src/main.ts --output dist/main
;;
*)
echo "Usage: deno-mycommand [start|build]"
exit 1
;;
esacRegister as Global Command:
chmod +x deno-mycommand
sudo ln -s $(pwd)/deno-mycommand /usr/local/bin/deno-mycommandPerformance Optimization Techniques
Cache Control Strategies:
# Disable cache in development
deno run --no-remote dev_server.ts
# Force cache in production
deno run --cached-only main.ts
# Clear cache directory
deno cache --reload --no-remoteV8 Engine Tuning:
# Enable TurboFan optimization
deno run --v8-flags=--turbofan app.ts
# Adjust heap memory limit
deno run --v8-flags=--max-old-space-size=4096 app.tsDebugging and Diagnostics
Debug Mode Usage:
# Start debug server
deno run --inspect=9229 app.ts
# Connect via Chrome DevTools
# chrome://inspect -> Configure -> Add port 9229Performance Analysis Tools:
# Generate CPU profile
deno run --v8-flags=--cpu-prof app.ts
# Generate heap snapshot
deno run --v8-flags=--heap-prof app.ts
# Analyze with Chrome DevTools
# Open chrome://inspect -> ProfilerCommon Issue Solutions
Permission Issue Troubleshooting
Error Example:
error: Uncaught PermissionDenied: read access to "/etc/passwd", run again with the --allow-read flagSolutions:
- Specify exact path:
deno run --allow-read=/etc/passwd app.ts - Use environment variable to restrict scope:
DENO_READ_PATHS="/etc/passwd" deno run app.ts
Dependency Cache Issues
Symptoms:
- Code not updated after dependency changes
- Inconsistent behavior across environments
Solutions:
# Force refresh all caches
deno cache --reload app.ts
# Clear specific dependency cache
deno cache --reload=https://deno.land/x/oak app.ts
# Delete global cache directory
rm -rf ~/.cache/denoCross-Platform Path Issues
Solution:
// Use Deno.path module for path handling
import { join } from "https://deno.land/std@0.224.0/path/mod.ts";
const configPath = join(Deno.cwd(), "config", "settings.json");Windows-Specific Handling:
# Run Deno in WSL2
wsl deno run --allow-read=/mnt/c/Users/app.ts
# Or configure Git Bash path conversion
export MSYS_NO_PATHCONV=1
deno run app.ts



