Prettier 02 ๐จ Installation โ Local vs. global install & editor integration
Prettier is a single npm package. Installing it is one command. The decision that matters is where it gets installed and how editors find it. A local install locks the version per project, ensures every contributor formats with the same printer, and lets CI run the exact same binary as the developer’s editor. A global install puts one prettier binary on the PATH and makes it available everywhere, but it introduces version drift between projects and between machines.
This chapter covers the installation boundary: local versus global, the exact commands for each, why Prettier’s documentation insists on local installation as the default, and how editor integrationsโVS Code, JetBrains IDEs, and othersโresolve the Prettier binary. It also covers the config and ignore files that make a project recognizable to both the CLI and the editor, and the verification steps that confirm the setup works before you write a single line of code.
Key point: Install Prettier locally in every project. Editor extensions are designed to pick up the local version, and the CLI in CI must match the editor on the developer’s machine. A global install is a convenience for ad-hoc formatting, not a substitute for a project dependency.
Why the install location matters
Version pinning is formatting stability. Prettier changes how it formats code between major releases. The documentation states this explicitly: “we change how code is formatted in each release” . A global install updates whenever the user runs npm update -g, which means a developer’s editor and the project’s CI can silently disagree about formatting. A local install with --save-exact pins the version in package.json and package-lock.json, so every environment resolves the same binary .
Editors follow the local install. The Prettier editor integration documentation warns: “It’s important to install Prettier locally in every project, so each project gets the correct Prettier version” . VS Code’s Prettier extension, JetBrains’ built-in support, and most other integrations resolve the local node_modules/prettier before falling back to a global binary. If no local install exists, the editor either refuses to format or uses a bundled version that may not match the project’s configuration.
The npx fallback is not a substitute. Running npx prettier without a local install causes npx to download the latest version temporarily . The result is a one-off format with a version the project does not control. This is acceptable for inspecting what Prettier would do, but unacceptable for committing formatted code. The version that produced the output is not recorded anywhere.
Global installs have a narrow use case. A global install is useful for formatting a file outside any project contextโa standalone script, a configuration file in a home directory, or a quick experiment. It is not useful for team projects, because the version is not shared, not pinned, and not reproducible in CI.
a. Local installation
The local install adds Prettier to the project’s devDependencies. The --save-exact flag (or --exact for yarn and pnpm) records the precise version in package.json without a caret range .
# npm
npm install --save-dev --save-exact prettier
# yarn
yarn add --dev --exact prettier
# pnpm
pnpm add --save-dev --save-exact prettier
# bun
bun add --dev --exact prettier
After installation, node_modules/.bin/prettier exists. Running it through the package manager’s exec mechanism uses that local binary:
# npm
npx prettier . --write
# yarn
yarn exec prettier . --write
# pnpm
pnpm exec prettier . --write
# bun
bunx prettier . --write
The --write flag reformats files in place. The --check flag reports which files are not formatted without modifying them . Both are common: --write during development, --check in CI.
b. Global installation
A global install places the prettier binary in the Node.js global prefix, typically /usr/local/bin/prettier or ~/.npm-global/bin/prettier. It becomes available in any directory without a project context.
# npm
npm install --global prettier
# yarn
yarn global add prettier
# pnpm
pnpm add --global prettier
The global binary can be invoked directly:
prettier --check .
prettier --write src/
JetBrains documentation lists both local and global install commands, but the Prettier project’s own guidance prioritizes the local install . The global install does not read the project’s package.json to determine a version; it uses whatever version was installed globally. If the global version is 3.x and the project depends on 2.x, the formatting output will differ.
c. Editor integration and version resolution
Editor extensions are the primary way most developers interact with Prettier. The extension does not bundle its own formatter; it looks for the project’s local Prettier installation first. This is deliberate: the editor should format with the same version that CI will check.
VS Code
The extension is called “Prettier – Code formatter” (publisher esbenp) . After installing the extension, configure it as the default formatter for the relevant languages:
{
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.formatOnSave": true
}
With formatOnSave enabled, every save triggers the extension to run the local Prettier binary against the file . The extension resolves Prettier from the workspace’s node_modules; if no local install exists, it either uses a bundled version or prompts to install one, depending on the extension version.
JetBrains IDEs (IntelliJ, WebStorm, Rider, PhpStorm)
JetBrains IDEs have built-in Prettier support. The IDE settings under Languages & Frameworks โ JavaScript โ Prettier allow specifying the Prettier package path. By default, the IDE auto-detects the local node_modules/prettier . The JetBrains documentation lists both npm install --save-dev --save-exact prettier and npm install --global prettier as installation options, but the IDE’s auto-detection prioritizes the local package when present .
Other editors
Vim, Emacs, Sublime Text, and Helix all have Prettier integrations. The pattern is the same: the integration runs a Prettier binary, and the recommended configuration points it at the local install . File watchers are a fallback for editors without native support: a watcher runs prettier --write on file changes, using the local binary.
d. Config and ignore files
The local install creates the binary. The config files tell Prettier and editors what to do with it.
An empty .prettierrc file is sufficient to signal that a directory is a Prettier project. The documentation recommends creating it with a cross-platform Node command to avoid shell-specific quoting issues :
node --eval "fs.writeFileSync('.prettierrc','{}\n')"
Options can be added later. The minimal set that teams commonly configure includes semi, singleQuote, printWidth, and trailingComma. The .prettierignore file lists paths that should not be formattedโbuild output, coverage reports, generated files:
node --eval "fs.writeFileSync('.prettierignore','# Ignore artifacts:\nbuild\ncoverage\n')"
Prettier also respects .gitignore rules if the files are in the same directory . This means node_modules/ and dist/ are ignored automatically in most setups, but an explicit .prettierignore makes the exclusions visible and reviewable.
Complete Example Session
# ============================================
# PART 1: LOCAL INSTALL WITH EXACT VERSION
# ============================================
# Install Prettier as a dev dependency, pinning the version
npm install --save-dev --save-exact prettier
# Verify the version in package.json
grep prettier package.json
# "prettier": "3.9.4" (no caret)
# ============================================
# PART 2: CREATE CONFIG AND IGNORE FILES
# ============================================
# Empty config signals a Prettier project
node --eval "fs.writeFileSync('.prettierrc','{}\n')"
# Ignore build artifacts and dependencies
node --eval "fs.writeFileSync('.prettierignore','# Ignore artifacts:\nbuild\ncoverage\ndist\n')"
# ============================================
# PART 3: RUN THE LOCAL BINARY
# ============================================
# Format all files using the local install
npx prettier . --write
# Check formatting without modifying files
npx prettier . --check
# ============================================
# PART 4: CONFIGURE VS CODE
# ============================================
# Install "Prettier - Code formatter" from the marketplace
# Add to settings.json:
# {
# "editor.defaultFormatter": "esbenp.prettier-vscode",
# "editor.formatOnSave": true
# }
# The extension resolves the local node_modules/prettier
# and formats on save with the pinned version.
# ============================================
# PART 5: CONFIGURE JETBRAINS IDE
# ============================================
# Settings โ Languages & Frameworks โ JavaScript โ Prettier
# Set "Prettier package" to <project>/node_modules/prettier
# Enable "On save" under "Run for files"
# The IDE uses the same local binary as the CLI and VS Code.
# ============================================
# PART 6: ADD A FORMAT SCRIPT
# ============================================
# Add to package.json scripts:
# "format": "prettier --write .",
# "format:check": "prettier --check ."
# Run with npm
npm run format
npm run format:check
# ============================================
# PART 7: CI CHECK
# ============================================
# In CI, run the check script after install
# npm ci
# npm run format:check
#
# This fails the build if any file is unformatted,
# using the exact version from package-lock.json.
# ============================================
# PART 8: GLOBAL INSTALL (OPTIONAL)
# ============================================
# For formatting files outside any project
npm install --global prettier
# The global binary is available anywhere
prettier --check ~/scripts/standalone.js
# Do not use the global binary inside a project
# that has a local install; use npx instead.
# ============================================
# PART 9: VERIFY THE SETUP
# ============================================
# Confirm the local binary resolves
npx prettier --version
# Output: 3.9.4
# Confirm the config is recognized
npx prettier --find-config-path src/index.js
# Output: /path/to/project/.prettierrc
# ============================================
# PART 10: CLEAN UP A GLOBAL-ONLY SETUP
# ============================================
# If a project relied on a global install, add the local one
npm install --save-dev --save-exact prettier
# Remove the global if it is no longer needed
npm uninstall --global prettier
# Verify the project uses the local binary
npx prettier --version
The ten parts covered local installation with exact versioning, config and ignore file creation, running the local binary, VS Code configuration, JetBrains configuration, npm scripts, CI checking, optional global install, setup verification, and migrating away from a global-only setup.
Quick Reference
Installation commands
| Method | Command | Use case |
|---|---|---|
| Local (npm) | npm install --save-dev --save-exact prettier | Team projects |
| Local (yarn) | yarn add --dev --exact prettier | Team projects |
| Local (pnpm) | pnpm add --save-dev --save-exact prettier | Team projects |
| Global (npm) | npm install --global prettier | Ad-hoc files outside projects |
Running Prettier
| Context | Command |
|---|---|
| Local (npm) | npx prettier . --write |
| Local (yarn) | yarn exec prettier . --write |
| Local (pnpm) | pnpm exec prettier . --write |
| Global | prettier . --write |
| CI check | npx prettier . --check |
Editor integrations
| Editor | Extension/Plugin | Resolution |
|---|---|---|
| VS Code | Prettier – Code formatter (esbenp) | Local node_modules/prettier |
| JetBrains | Built-in | Auto-detects local package |
| Vim | vim-prettier | Configurable path |
| Emacs | prettier.el | Configurable path |
| Sublime Text | JsPrettier | Configurable path |
Config files
| File | Purpose | Creation |
|---|---|---|
.prettierrc | Formatting options | node --eval "fs.writeFileSync('.prettierrc','{}\n')" |
.prettierignore | Files to exclude | node --eval "fs.writeFileSync('.prettierignore','build\ncoverage\n')" |
Best Practices
โ Do This:
# Install locally with exact versioning in every project
npm install --save-dev --save-exact prettier # โ
# Create an empty .prettierrc to signal project config
node --eval "fs.writeFileSync('.prettierrc','{}\n')" # โ
# Configure the editor to use the local Prettier binary
"editor.defaultFormatter": "esbenp.prettier-vscode" # โ
# Add format scripts to package.json
"format": "prettier --write ." # โ
# Run --check in CI with the locked version
npm ci && npx prettier . --check # โ
โ Don’t Do This:
# Rely on a global install for project formatting
prettier --write . # โ (version not pinned)
# Skip the local install and let npx download a temporary version
npx prettier . --write # โ (unpinned version)
# Install globally and expect editors to use it
npm install --global prettier # โ (editors prefer local)
# Commit without a .prettierignore
# node_modules may be formatted accidentally # โ
# Use different Prettier versions in editor and CI
# Formatting conflicts and failed checks follow # โ
Common Pitfalls
| Pitfall | Why It Happens | Fix |
|---|---|---|
| Editor formats differently than CLI | Editor uses a global or bundled Prettier version | Install locally and point the editor at node_modules/prettier |
| CI fails with formatting differences | Developer’s global version differs from project version | Use local install and --save-exact; run CI with npm ci |
npx prettier downloads a version unexpectedly | No local install exists | Run npm install --save-dev --save-exact prettier first |
| Editor does not format on save | Default formatter not set | Configure editor.defaultFormatter in VS Code settings |
node_modules gets formatted | No .prettierignore and no .gitignore | Create .prettierignore with node_modules/ |
| Config changes have no effect | Prettier is not finding the .prettierrc | Run npx prettier --find-config-path <file> to verify |
Real-World Examples
1. Local install with exact version
npm install --save-dev --save-exact prettier
2. Format all files with local binary
npx prettier . --write
3. Check formatting in CI
npx prettier . --check
4. VS Code format-on-save
{ "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true }
5. JetBrains auto-detect local package
Settings โ Languages & Frameworks โ JavaScript โ Prettier
Package: <project>/node_modules/prettier
6. Empty config file
node --eval "fs.writeFileSync('.prettierrc','{}\n')"
7. Ignore file with build output
node --eval "fs.writeFileSync('.prettierignore','build\ncoverage\ndist\n')"
8. Format script in package.json
{ "scripts": { "format": "prettier --write .", "format:check": "prettier --check ." } }
9. Global install for ad-hoc use
npm install --global prettier && prettier --check ~/notes/script.js
10. Verify config resolution
npx prettier --find-config-path src/index.js
Visual
Local vs. global resolution
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ HOW PRETTIER IS RESOLVED โ
โ โ
โ Editor (VS Code, JetBrains) โ
โ โ โ
โ โโโ Look for local node_modules/prettier โโโบ found? โ
โ โ โ โ
โ โ โโโ yes โโโบ use it (project version) โ
โ โ โ โ
โ โ โโโ no โโโบ use bundled/global fallback โ
โ โ โ
โ CLI (npx) โ
โ โ โ
โ โโโ Look for local node_modules/.bin/prettier โโโบ found?โ
โ โ โ โ
โ โ โโโ yes โโโบ run it (project version) โ
โ โ โ โ
โ โ โโโ no โโโบ npx downloads latest temporarily โ
โ โ โ
โ The local install is the anchor. Everything else is fallback.โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
The three installation outcomes
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ INSTALLATION OUTCOMES โ
โ โ
โ LOCAL + EXACT โ
โ package.json: "prettier": "3.9.4" โ
โ Editor: uses 3.9.4 โ
โ CI: uses 3.9.4 โ
โ Result: identical formatting everywhere โ
โ โ
โ LOCAL, NO EXACT โ
โ package.json: "prettier": "^3.9.4" โ
โ npm install: may resolve 3.10.0 โ
โ CI with npm ci: uses lockfile version โ
โ Result: mostly consistent, but lockfile drift risk โ
โ โ
โ GLOBAL ONLY โ
โ package.json: no prettier โ
โ Editor: bundled or global version โ
โ CI: npx downloads latest โ
โ Result: version drift, formatting conflicts โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Editor resolution flow
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ EDITOR PRETTIER RESOLUTION โ
โ โ
โ VS Code / JetBrains โ
โ โ โ
โ โผ โ
โ Check workspace node_modules/prettier โ
โ โ โ
โ โโโ Found โโโบ Use that binary โ
โ โ โ
โ โโโ Not found โโโบ Check global install โ
โ โ โ
โ โโโ Found โโโบ Use global โ
โ โ โ
โ โโโ Not found โโโบ Error/prompt โ
โ โ
โ The local install wins. Always. โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Project structure after local install
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ PROJECT ROOT โ
โ โ
โ package.json โ "prettier": "3.9.4" (exact) โ
โ package-lock.json โ locked resolution โ
โ .prettierrc โ {} (formatting options) โ
โ .prettierignore โ build, coverage, dist โ
โ node_modules/ โ
โ โโโ .bin/ โ
โ โโโ prettier โ local binary โ
โ โ
โ Editor reads .prettierrc, runs node_modules/.bin/prettier. โ
โ CI runs npx prettier, same binary, same config. โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Summary
| Item | Value |
|---|---|
| Recommended install | Local, with --save-exact |
| Local install command (npm) | npm install --save-dev --save-exact prettier |
| Global install command | npm install --global prettier |
| Local run command | npx prettier . --write |
| CI check command | npx prettier . --check |
| Config file | .prettierrc (empty {} is valid) |
| Ignore file | .prettierignore |
| VS Code extension | esbenp.prettier-vscode |
| Editor resolution priority | Local node_modules/prettier first |
| Version stability mechanism | --save-exact + package-lock.json |
Key takeaways:
- Local installs are the default for a reason. They pin the version per project, ensure editor and CI use the same binary, and make formatting reproducible across machines.
--save-exactprevents version drift. A caret range (^3.9.4) allows minor updates that may change formatting output. The exact version inpackage.jsonis the contract.- Editors resolve the local binary first. VS Code and JetBrains both look for
node_modules/prettierbefore falling back to a global install. The local install is what the editor formats with. - Global installs are for ad-hoc use only. They do not read project configuration, do not respect the project’s pinned version, and should not be used inside a project that has a local install.
- The
npxfallback is a warning sign. Ifnpx prettierdownloads a version instead of running the local one, the local install is missing. Fix it before committing formatted code. - Config and ignore files make the project recognizable. An empty
.prettierrcsignals that Prettier is configured. A.prettierignoreprevents build artifacts from being formatted and committed.
Remember: The installation decision is not about convenience; it is about reproducibility. A global Prettier formats code. A local Prettier formats code with the version the project agreed on. The difference only becomes visible when a formatting change lands in a minor release and the global install updates while the project stays pinned. By then, the formatted code is already in the repository, and the CI check is failing for reasons that have nothing to do with the developer’s changes. Install locally, pin exactly, configure the editor to use the local binary, and let the global install remain a tool for files that live outside any project.
Stop using slow, ad-bloated tool sites! ๐คฎ
๐ Search “KandZ Tools” on Google to use many professional utilities for free.
KandZ.me is the ultimate minimalist hub for:
โ
Finance (Mortgage, Interest, Inflation)
โ
Tech (Base64, JSON, Dev Suite, IP)
โ
Health (BMI, BMR, TDEE)
โ
Productivity (Timer, Workspace, QR)
โก๏ธ Fast & Private
๐ No data leaves your device
๐ 100% Free
๐ Use it now: https://tools.kandz.me
๐ Bookmark itโyouโll need it later!