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:- Load Prettier configuration from
.prettierrc - Scan all source files in your project
- Automatically format files according to configured rules
- Save the formatted files
- 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)
.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:- Code Style
- Spacing
- Trailing Items
- Special
- 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: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 betweenfmt and lint:
| Aspect | native-desktop fmt | native-desktop lint |
|---|---|---|
| Purpose | Code formatting | Code quality & patterns |
| Tool | Prettier | ESLint |
| Auto-Fix | ✅ Yes (always) | ⚠️ Limited |
| Focus | Style consistency | Logic & best practices |
| Concerns | Indentation, quotes, spacing | Unused variables, type safety |
| Conflicts | Can conflict with ESLint | Can conflict with Prettier |
Common Issues
Prettier Configuration Not Found
Prettier Configuration Not Found
Error:
.prettierrc not foundSolution: Ensure the configuration file exists in your project root.Syntax Errors Prevent Formatting
Syntax Errors Prevent Formatting
Error:
SyntaxError: Unexpected tokenSolution: Fix syntax errors in your code before formatting.Conflicting ESLint and Prettier Rules
Conflicting ESLint and Prettier Rules
Error: ESLint and Prettier disagree on formattingSolution: Install Update
eslint-config-prettier to disable conflicting ESLint rules..eslintrc.json:Files Not Being Formatted
Files Not Being Formatted
Error: Some files are not formattedSolution: Check
.prettierignore and ensure files aren’t excluded.Format Changes Not Saved
Format Changes Not Saved
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:- Open VS Code
- Go to Extensions (Cmd+Shift+X / Ctrl+Shift+X)
- Search for “Prettier - Code formatter”
- Install the official Prettier extension
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: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
| Command | Tool | Purpose | Changes Files |
|---|---|---|---|
native-desktop fmt | Prettier | Format code style | ✅ Yes |
native-desktop lint | ESLint | Check code quality | ❌ No |
native-desktop types | TypeScript | Check types | ❌ No |
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