How to Use deno fmt to Format Your Code Easily
Use the
deno fmt command in your terminal to automatically format your Deno project files. It fixes indentation, spacing, and style issues to keep your code clean and consistent.Syntax
The basic syntax of deno fmt is simple and straightforward:
deno fmt: Formats all supported files in the current directory and its subdirectories.deno fmt [file_or_directory]: Formats specific files or directories you specify.deno fmt --check: Checks if files are formatted without changing them, useful for CI pipelines.
bash
deno fmt [options] [files...]
Example
This example shows how to format a single file and then check formatting without changing files.
bash
deno fmt example.ts deno fmt --check example.ts
Output
Formatting example.ts
All files are formatted correctly.
Common Pitfalls
Some common mistakes when using deno fmt include:
- Running
deno fmtin the wrong directory, so no files get formatted. - Expecting
deno fmtto fix syntax errors; it only formats code style. - Not using
--checkin CI, which can miss unformatted files.
Always ensure you run deno fmt where your code files are and use --check to verify formatting without changes.
bash
Wrong: deno fmt /wrong/path Right: deno fmt ./src
Quick Reference
| Command | Description |
|---|---|
| deno fmt | Format all supported files in current directory recursively |
| deno fmt file.ts | Format a specific file |
| deno fmt dir/ | Format all files in a specific directory |
| deno fmt --check | Check if files are formatted without changing them |
| deno fmt --help | Show help information about formatting options |
Key Takeaways
Run
deno fmt in your project root to format all code files automatically.Use
deno fmt --check to verify formatting without modifying files, ideal for CI.Specify files or directories after
deno fmt to format only those targets.deno fmt fixes style issues but does not correct syntax errors.Always run
deno fmt where your code files are located to see results.