Last modified: Jul 20, 2025 By Alexander Williams
Install Prettier in Node.js - Quick Guide
Prettier is a popular code formatter. It helps maintain consistent code style. This guide shows how to install it in Node.js.
Prerequisites
Before installing Prettier, ensure you have Node.js installed. You can check by running:
node -v
If Node.js is not installed, download it from the official website. Also, basic knowledge of npm is helpful.
Install Prettier
To install Prettier, open your terminal. Navigate to your project directory. Run this command:
npm install --save-dev prettier
This installs Prettier as a dev dependency. The --save-dev
flag adds it to your devDependencies in package.json.
Basic Configuration
Create a .prettierrc
file in your project root. This file stores your formatting rules. Here's a basic example:
{
"semi": true,
"singleQuote": true,
"tabWidth": 2
}
This configuration enforces semicolons, single quotes, and 2-space tabs. You can customize these rules as needed.
Format Your Code
To format your code, run Prettier with this command:
npx prettier --write .
The --write
flag applies changes to files. The dot formats all files in the current directory.
Integrate with ESLint
If you use ESLint, you can integrate Prettier. First, install these packages:
npm install --save-dev eslint-config-prettier eslint-plugin-prettier
Then update your ESLint config. This prevents conflicts between ESLint and Prettier rules.
Add a Script
Add a format script to your package.json for convenience:
"scripts": {
"format": "prettier --write ."
}
Now you can run npm run format
to format your code. This saves time and ensures consistency.
Ignore Files
Create a .prettierignore
file to exclude files. This works like .gitignore. Example:
node_modules
build
*.min.js
This ignores node_modules, build folders, and minified JS files. Add any files you don't want formatted.
Editor Integration
For best results, integrate Prettier with your editor. Most popular editors support Prettier plugins. This formats code automatically on save.
Conclusion
Prettier simplifies code formatting in Node.js projects. It ensures consistent style across your team. Combined with tools like ESLint, it improves code quality.
Install Prettier today to save time on formatting. Focus on writing code while Prettier handles the style.