Format Astro Files in VSCode with Prettier

If you're like me, you want your code to look as clean and consistent as possible. That's why I use Prettier to format my Astro files. Prettier is an opinionated code formatter that takes your code and reformats it so that it conforms to a set of style rules. This can be really helpful if you're working on a project with multiple developers, because everyone's code will look the same. In this blog post, I'm going to show you how to format Astro files in VSCode with Prettier.

Format Astro Files in VSCode with Prettier

Unfortunately, Prettier does not work with Astro files out of the box. To make it work, we need to install an extension in our Astro project first. For this, we use the prettier-plugin-astro NPM package.

To be able to automatically Format Astro Files in VSCode with Prettier, you have to make sure format on save is enabled. To enable it, make sure the following line is present in your settings.json file:

"editor.formatOnSave": true,
Code language: JSON / JSON with Comments (json)

Installing Prettier-Plugin-Astro

Inside your project folder, run the following command:

npm install -D prettier-plugin-astro
Code language: Bash (bash)

Configure VSCode for Astro / Prettier Auto Formatting

Now we need to tell VSCode which formatter to use for our Astro files:

  1. Make sure the Prettier extension for VSCode is installed.
  2. Open the Command Palette (View -> Command Palette or Cmd/Ctrl + Shift + P).
  3. Type 'settings' and select 'Preferences: Open Settings (JSON)'. This will open your VSCode settings file.
  4. Add the following lines to your settings file:
"[astro]": { "editor.defaultFormatter": "astro-build.astro-vscode" }
Code language: JavaScript (javascript)
  1. Save the file and close it.
  2. Open your Astro file that you want to format and press CTRL + S to save and see if it works.

If it doesn't work yet, there might be another step involved (which has been the case for me). With your Astro file open, check the bottom of your VSCode window. You should see a symbol like this:

Format Astro Files in VSCode with Prettier

Click on this icon. Now VSCode should offer you some options as to which formatter to use. You can now either choose Prettier, or the built-in Astro formatter. After doing this final step, no matter which formatter you choose, your files should be formatted automatically.

VS Code hide files and folders from...
VS Code hide files and folders from the explorer

Conclusion

Formatting your Astro files with Prettier is a great way to ensure that your code is clean and consistent across all of your files. By following the steps outlined in this blog post, you can easily format your files in VSCode with just a few clicks. Give it a try and see for yourself how much easier it is to work with formatted code!

🧑‍💻 Learn Web Development
👉 Top Programming Languages in 2022
👉 The Fresh JavaScript Framework
🚀 NextJS
👉 NextJS and Tailwind CSS - Complete Beginner Guide
⚛️ React
👉 Build an Input Form with React Hooks
👉 React Styled Components - The Complete Guide

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Share via
Copy link
Powered by Social Snap