How to use prettier to customize your code formatting

How to use prettier to customize your code formatting

Learn to configure and use this code formatter to make your life easier. This means you can configure Prettier via (in order of precedence): ... Usage. Install · Ignoring Code · Integrating with Linters · Pre-commit Hook · Plugins ... the location of the file being formatted, and searching up the file tree until a config file ... The options you can use in the configuration file are the same as the API options.

Tabs or spaces? Double or single quotes? Trailing commas and whitespace? It doesn’t matter, while you’re being consistent and using the same pattern throughout your code. However, getting warnings and errors about these small things can get pretty annoying pretty fast. That’s why you should use a code formatter and configure it to format a file as soon as you save it, and then you can take care of the important stuff. There are a lot of options out there but this week I want to talk about prettier, a light and simple tool that offers support to a lot of languages and most editors.

1. Install prettier in your project

npm:npm install --save-dev --save-exact prettier

yarn:yarn add --dev --exact prettier

After installing, create a json file to store your custom configurations:

echo {}> .prettierrc.json

You should also create a .prettierignore file for everything that you don’t want to format. You can use your .gitignorefile as a reference for this step.

2. Install the prettier extension in your editor

Search and install this extension on vscode:

Image for post

Image for post

You can configure it to format on save, which is really useful. For this, open the palette with ctrl shift P and go to user settings. Go to formatting and select option format on save.

3. Set up your format options

Now let’s come back to our prettierrc.json file and add our new configs. Prettier doesn’t have a huge amount of options, but that’s precisely the point — avoiding endless debates and optimizing time. For this example, let’s see how to set up tabs instead of spaces, single quotes, trailing comma and bracket spacing:

Image for post

Image for post

Use tabSize to define how many spaces should occupy a tab, and if you want you can use the option “parser” — but prettier automatically infers it from the input file path, so this isn’t super important. Check the documentation for the full list of options.

web-development programming-tips code junior-developer javascript

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

13 Useful JavaScript Developer Tools for Writing High-Quality Code

Today I’m going to show you 13 well-know and popular tools to write better and cleaner JavaScript code. These are tools that I’m using right now on my JavaScript developer journey. So if you want to improve the quality of your projects, this list is for you.

Principles of Functional Programming in JavaScript That Will Make Your Coding Life Easier

Who else loves to write side-effects-free functions? I think we, as programmers, all do. Today, in this story, I will walk you through the basic principles of functional programming that will make your coding life easier.

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

How to start writing less error-prone code in JavaScript

How to start writing less error-prone code in JavaScript - Everything in JavaScript is an object!’. We said that this assertion is false. Many things in JavaScript can behave like an object, but that doesn’t mean it’s the object. We can say we have three types of objects (objects, functions and arrays) in JavaScript.

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...