Skip to main content
The fmt command runs Prettier on your Native Desktop project to automatically format your code, ensuring consistent style and formatting across your entire codebase.

Prerequisites

Before running the format command, ensure:
  • Your project has been created using native-desktop create
  • All dependencies are installed (npm install)
  • Prettier configuration exists (.prettierrc)

Basic Usage

To format your project, open the terminal in the root directory of your Native Desktop project and run:
This command will:
  1. Load Prettier configuration from .prettierrc
  2. Scan all source files in your project
  3. Automatically format files according to configured rules
  4. Save the formatted files
  5. Report the number of files formatted

What Gets Formatted

By default, the format command processes:
  • All TypeScript files (.ts, .tsx)
  • All JavaScript files (.js, .jsx)
  • JSON files (.json, .json5)
  • Markdown files (.md, .mdx)
  • CSS/SCSS files (.css, .scss)
  • HTML files (.html)
  • YAML files (.yml, .yaml)
Files and directories specified in .prettierignore are automatically excluded from formatting.

Output Examples

Successful Format

No Changes Needed

With Errors

Prettier Configuration

The format command uses the .prettierrc file generated by native-desktop create. Here’s an example configuration:

Configuration Options

Common Prettier options you can customize:
  • semi: Add semicolons at the end of statements (default: true)
  • singleQuote: Use single quotes instead of double quotes (default: true)
  • quoteProps: When to quote properties in objects
  • jsxSingleQuote: Use single quotes in JSX (default: false)

Formatting Examples

Before Formatting

After Formatting

Ignoring Files

Create or edit .prettierignore to exclude files from formatting:

Ignoring Code Blocks

You can ignore specific code blocks in your files:
Or ignore entire files:

Integration with Development Workflow

Pre-Commit Hook

Format code automatically before commits using Husky:

CI/CD Pipeline

Check formatting in your continuous integration:

npm Scripts

Add formatting to your package.json scripts:

Format vs Lint

Understanding the difference between fmt and lint:
Use both commands together: native-desktop fmt for style, native-desktop lint for quality.

Common Issues

Error: .prettierrc not foundSolution: Ensure the configuration file exists in your project root.
Error: SyntaxError: Unexpected tokenSolution: Fix syntax errors in your code before formatting.
Error: ESLint and Prettier disagree on formattingSolution: Install eslint-config-prettier to disable conflicting ESLint rules.
Update .eslintrc.json:
Error: Some files are not formattedSolution: Check .prettierignore and ensure files aren’t excluded.
Error: Files appear formatted but changes aren’t savedSolution: Check file permissions and ensure you have write access.

Customizing Prettier

You can customize Prettier rules in .prettierrc:

Language-Specific Overrides

You can apply different rules to different file types:

IDE Integration

Visual Studio Code

Install the Prettier extension:
  1. Open VS Code
  2. Go to Extensions (Cmd+Shift+X / Ctrl+Shift+X)
  3. Search for “Prettier - Code formatter”
  4. Install the official Prettier extension
Configure auto-format on save:

WebStorm / IntelliJ IDEA

Prettier is built-in. Enable it in: Settings → Languages & Frameworks → JavaScript → Prettier
  • Check “Automatic Prettier configuration”
  • Check “Run on save for files”

Sublime Text

Install the JsPrettier package via Package Control.

Best Practices

Format Frequently

Run formatting regularly during development to maintain consistency.

Commit Formatted Code

Always format code before committing to keep the repository clean.

Team Consistency

Share the same Prettier configuration across your entire team.

Automate Formatting

Set up automatic formatting in your IDE and CI/CD pipeline.

Checking Without Formatting

To check if files need formatting without modifying them:
This is useful for CI/CD pipelines where you want to verify formatting without making changes.

Format Specific Files

While the CLI command formats the entire project, you can format specific files using npx:

Development Workflow

Recommended workflow for code formatting:

Performance Tips

Use .prettierignore

Exclude large directories to speed up formatting.

Format Changed Files

In large projects, format only modified files.

Cache Results

Prettier caches results for faster subsequent runs.

IDE Formatting

Use IDE auto-format for immediate feedback.

Command Comparison

For best results, run all three commands as part of your development workflow.

Exit Codes

The format command uses standard exit codes:
  • 0: Files formatted successfully
  • 1: Errors occurred during formatting