Windows环境下配置package.json中Prettier脚本遇文件匹配错误求助
This error pops up because Windows' default command shells (CMD and PowerShell) don’t support the brace expansion syntax ({app,__{tests,mocks}__}) that works smoothly on Mac/Ubuntu’s bash/zsh. When you run the command on Windows, the shell doesn’t expand those braces into actual file paths—so Prettier ends up searching for a literal path with braces and comes up empty.
Here are several straightforward fixes to get your Prettier script working across all operating systems:
1. Split Path Patterns Explicitly
Instead of relying on brace expansion, list each target directory separately. Prettier accepts multiple path arguments, so this works reliably on Windows and Unix-like systems alike:
Update your package.json scripts section to:
"scripts": { "format": "prettier --single-quote --trailing-comma es5 --write \"app/**/*.js\" \"__tests__/**/*.js\" \"__mocks__/**/*.js\"" }
Note the escaped double quotes (\")—these are required because JSON uses double quotes to wrap values, so we need to escape inner quotes for the shell to interpret them correctly.
2. Move Rules to a Prettier Config File (Cleaner Approach)
To simplify your CLI command and avoid repeating formatting rules, create a .prettierrc file in your project root with your preferences:
{ "singleQuote": true, "trailingComma": "es5" }
Then your package.json script can be trimmed down to:
"scripts": { "format": "prettier --write \"app/**/*.js\" \"__tests__/**/*.js\" \"__mocks__/**/*.js\"" }
This keeps your scripts tidy and makes updating formatting rules much easier later on.
3. Use a Unix-Compatible Shell on Windows
If you prefer sticking with the original brace syntax, run the command in a shell that supports Unix-style expansions:
- Git Bash: If you have Git installed on Windows, open Git Bash and run
npm run formatdirectly—Git Bash will handle the brace expansion correctly. - WSL (Windows Subsystem for Linux): Run your project inside a WSL environment; it behaves exactly like Ubuntu, so your original command will work without any changes.
Quick Tips
- Windows accepts both
/and\as path separators, so you don’t need to modify the**/*.jsglob patterns. - When using PowerShell, just run
npm run formatnormally—it will handle the escaped quotes from yourpackage.jsoncorrectly.
内容的提问来源于stack exchange,提问作者locropulenton

