How to Set Up Prettier in Visual Studio Code
Writing clean and readable code is essential, whether you’re working alone or with a team of developers. While many factors contribute to code readability, one of the most important is consistent code formatting.
But here’s the problem: manual code formatting can be an absolute pain and very prone to error. Tools like Prettier make formatting HTML, CSS, JavaScript, and other languages, so much easier. Discover how to install and use the Prettier extension for code formatting, as well as some advanced configuration settings.

Installing Prettier
Before proceeding, ensure that you have Node.js installed on your computer. You can install the latest version from theofficial Node.js downloads page. It comes withthe node package manager (npm)built-in, which you’ll use to manage your Node.js packages.
After confirming that Node.js is installed locally, start by creating an empty directory for your project. it’s possible to name the directoryprettier-demo.

Next, cd into that directory using a command line, then run the following command to initialize a Node.js project:
This command generates a package.json file containing the default settings.

To install the Prettier extension, run this terminal command:
The–save-devflag installs prettier as a dev dependency, meaning it’s only used during development.

Now that you have it installed, you can start exploring how Prettier works by using it on the command line.
Using Prettier via the Command Line
Start by creating ascript.jsfile and populating it with the following code:
To format the code in this script.js file via the command line, run the following command:

The command reformats the JavaScript code in script.js to Prettier’s default standard. This is going to be the result:
you could also format HTML markup from the command line. Create anindex.htmlfile in the same directory asscript.js. Then paste the following poorly-formatted HTML into the file:
To format the HTML, run this command:
This command reformats the HTML to Prettier’s default standard, which results in the following code:
You can also use the–checkflag to check if the code conforms to Prettier’s standards. The following example checksscript.js:
This is useful if you want a pre-commit hook to ensure people are using Prettier and formatting the files before pushing them to Git. This works well whencontributing to open source.
Integrating Prettier Into Visual Studio Code
Using Prettier via the command line can be a pain. Instead of manually running a command each time you want to format code, you can set it up to format automatically when you change a file. Luckily, Visual Studio Code (VS Code) has a way built-in to do this for you.
Go to theExtensionstab in VS Code and search forPrettier. Click onPrettier - Code formatter, install it, then enable it.
Go into your VS Code settings by navigating toFile > Preferences > Settings. In the search box, search forPrettier. You’ll find a ton of options to help you configure the Prettier extension.
Typically, you can get by with the default settings. The only thing you might consider changing is the semicolons (you can remove them if you want). Otherwise, everything is set to default, but you can change it however you want.
Be sure to enable theformatonsaveoption so that the code in each file is automatically formatted when you save that file. To enable it, just search forformatonsaveand tick the box.
If you’re not using VSCode or the extension isn’t working for some reason, you candownload the onchange library. This runs the command to format the code anytime you change the file.
How to Ignore Files When Formatting With Prettier
If you were to run the prettier–writecommand on your entire folder, it would go through every single one of your node modules. But you shouldn’t be wasting time formatting other people’s code!
To get around this problem, create a.prettierignorefile and include the termnode_modulesin the file. If you were to run the–writecommand on the entire folder, it’d reformat all the files except those in thenode_modulesfolder.
you could also ignore files with a specific extension. For example, if you want to ignore all HTML files, simply add*.htmlto.prettierignore.
How to Configure Prettier
You can configure how you want Prettier to work with different options. One way is to add aprettierkey to yourpackage.jsonfile. The value will be an object containing all the configuration options:
The second option (which we recommend) is to create a.prettierrcfile. This file will allow you to do all sorts of customizations.
Let’s say you don’t like semicolons. you could remove them by placing the following object in the file:
Theoverridesproperty allows you to define custom overrides for certain files or file extensions. In this case, we say that all files ending in.ts(that is, typescript files) should have no semicolons.
Using Prettier With ESLint
ESLint is a linting tool for detecting errors in JavaScript code as well as formatting it. If you’re using Prettier, you probably wouldn’t want to use ESLint for formatting as well. To use them together, you’ll need to install and set upeslint-config-prettier. This tool turns off all the ESLint configurations for things that Prettier already handles.
First, you need to install it:
Next, add it to the extends list in the.eslintrcfile (make sure it’s the last thing in the list):
Now ESLint will disable all rules that Prettier is already taking care of to prevent conflicts.
Clean Up Your Codebase With Prettier and ESLint
Prettier is an ideal tool to clean up your code and enforce consistent formatting within a project. Setting it to work with VS Code means it’s always within reach.
ESLint is a must-have JavaScript tool that goes hand-in-hand with Prettier. It provides a ton of features and customization options that go beyond basic formatting. Learn how to use ESLint with JavaScript if you want to be a more productive developer.
Make your code base clean and consistent with this tool.
Don’t let aging hardware force you into buying expensive upgrades.
Windows is great, but adding this makes it unstoppable.
These films will leave you questioning humanity, but also wanting more.
You can block out the constant surveillance and restore your privacy with a few quick changes.
I found my TV was always listening—so I shut it down.