0
0
DenoHow-ToBeginner ยท 3 min read

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 fmt in the wrong directory, so no files get formatted.
  • Expecting deno fmt to fix syntax errors; it only formats code style.
  • Not using --check in 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

CommandDescription
deno fmtFormat all supported files in current directory recursively
deno fmt file.tsFormat a specific file
deno fmt dir/Format all files in a specific directory
deno fmt --checkCheck if files are formatted without changing them
deno fmt --helpShow 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.