The deno fmt command formats source files using Deno’s opinionated code formatter based on dprint.
Usage
deno fmt [OPTIONS] [FILES]...
Basic Examples
# Format all supported files in current directory
deno fmt
# Format specific files
deno fmt src/main.ts
# Format multiple files
deno fmt src/ tests/
# Check if files are formatted (don't write)
deno fmt --check
# Format stdin
cat file.ts | deno fmt -
Supported File Types
Deno’s formatter supports:
- TypeScript (
.ts, .tsx, .mts, .cts)
- JavaScript (
.js, .jsx, .mjs, .cjs)
- JSON (
.json, .jsonc)
- Markdown (
.md, .markdown)
Check Mode
Check if files are formatted without writing changes# Check formatting
deno fmt --check
# Exit code 0 if formatted, 1 if not
deno fmt --check src/
Stop checking after first unformatted filedeno fmt --check --fail-fast
Watch Mode
Watch for file changes and format automatically# Watch mode
deno fmt --watch
# Watch specific paths
deno fmt --watch=src/
Don’t clear the screen on watch mode restart
Use tabs instead of spaces for indentation
Set the maximum line width (default: 80)deno fmt --line-width=120
Set the number of spaces for indentation (default: 2)deno fmt --indent-width=4
Use single quotes instead of double quotes
How to wrap prose in markdown (always, never, preserve)deno fmt --prose-wrap=always
Don’t use semicolons except where necessary
File Filtering
Ignore files or directoriesdeno fmt --ignore=build/,dist/
Configuration
Formatting options can be configured in deno.json:
{
"fmt": {
"useTabs": false,
"lineWidth": 80,
"indentWidth": 2,
"singleQuote": false,
"proseWrap": "preserve",
"include": ["src/"],
"exclude": ["dist/", "build/"]
}
}
Ignoring Code
Ignore File
Add a comment at the top of the file:
// deno-fmt-ignore-file
const unformatted = "code";
Ignore Next Line
// deno-fmt-ignore
const unformatted = "code";
Ignore Range
// deno-fmt-ignore-start
const unformatted = "code";
let another = "value";
// deno-fmt-ignore-end
Examples
# Format only TypeScript files
deno fmt **/*.ts
# Format only JSON files
deno fmt **/*.json
CI/CD Integration
# Check formatting in CI
deno fmt --check || exit 1
# Format and show diff
deno fmt --check 2>&1 | tee format-report.txt
Pre-commit Hook
#!/bin/bash
# .git/hooks/pre-commit
if ! deno fmt --check; then
echo "Please run 'deno fmt' before committing"
exit 1
fi
# Format with custom line width and single quotes
deno fmt --line-width=120 --single-quote src/
# Format with tabs
deno fmt --use-tabs --indent-width=4 src/