Prettier Plugin for Vue Single File Components (SFCs)

raw JSON →
1.1.6 verified Sat Apr 25 auth: no javascript

prettier-plugin-vue is a Prettier plugin designed to enhance the formatting capabilities for Vue Single File Components (SFCs) beyond Prettier's default behavior. Currently at version 1.1.6, this plugin enables developers to precisely control which blocks within an SFC are formatted by Prettier. Its primary differentiator is the `vueExcludeBlocks` option, which allows specific sections (like `<style>` or `<template>`) to be ignored during formatting. This feature is particularly useful for integrating Prettier with other specialized linters such as `eslint` and `stylelint`, preventing formatting conflicts that might arise from multiple tools acting on the same code sections. The plugin autoloads in most setups, streamlining its adoption. While not explicitly stating a release cadence, its presence on npm and active GitHub actions suggest ongoing maintenance, providing a stable solution for Vue formatting challenges. It avoids common issues with IDE-specific formatters by handling exclusions at the Prettier configuration level.

error Error: Cannot find module 'prettier-plugin-vue'
cause The plugin is either not installed as a dependency or Prettier cannot locate it, which can happen with certain package managers (pnpm, Yarn PnP) that use non-standard `node_modules` structures.
fix
First, ensure prettier-plugin-vue is installed as a devDependency (npm install --save-dev prettier-plugin-vue). Then, explicitly configure it in your prettier.config.js: module.exports = { plugins: [require('prettier-plugin-vue')] }.
error Prettier is not formatting my Vue <style> block, even when I run it.
cause The `prettier-plugin-vue` defaults to excluding `<style>` blocks from formatting to avoid interfering with dedicated CSS linters like `stylelint`.
fix
To enable Prettier to format <style> blocks, you need to override the default configuration. In your prettier.config.js, set vueExcludeBlocks to an empty array or remove 'style' from it: module.exports = { vueExcludeBlocks: [] }.
error SyntaxError: Cannot use import statement outside a module (when configuring Prettier)
cause Your `prettier.config.js` file is likely being treated as a CommonJS module, which does not support ESM `import` statements at the top level.
fix
Use CommonJS require() syntax for loading plugins in prettier.config.js: module.exports = { plugins: [require('prettier-plugin-vue')] }. If you prefer ESM syntax, rename your configuration file to prettier.config.mjs.
gotcha Prettier's plugin autoloading mechanism may not function correctly with certain package managers, specifically pnpm or Yarn PnP, requiring explicit configuration.
fix To ensure the plugin is loaded, explicitly add `prettier-plugin-vue` to your Prettier configuration's `plugins` array. For CommonJS configs: `plugins: [require('prettier-plugin-vue')]`. For ESM configs: `plugins: ['prettier-plugin-vue']`.
gotcha By default, `prettier-plugin-vue` excludes `<style>` blocks from Prettier's formatting process. This is done to prevent potential conflicts with dedicated CSS linters like `stylelint`.
fix If you wish for Prettier to format `<style>` blocks within your Vue SFCs, you must explicitly remove `'style'` from the `vueExcludeBlocks` array in your Prettier configuration (e.g., `vueExcludeBlocks: []`). Be aware of potential conflicts if you are also using a style linter.
gotcha The `vueExcludeBlocks` option expects an array of strings, where each string corresponds to the name of a block type (e.g., `'style'`, `'template'`, `'script'`) you wish to exclude from formatting.
fix Ensure that `vueExcludeBlocks` is correctly defined as an array of string literals in your `.prettierrc` or `prettier.config.js` file, for example: `vueExcludeBlocks: ['style', 'template']`.
npm install prettier-plugin-vue
yarn add prettier-plugin-vue
pnpm add prettier-plugin-vue

This quickstart guides you through installing the plugin, configuring Prettier to exclude style blocks in Vue SFCs, and then demonstrates running Prettier on a sample Vue file to show which blocks are formatted and which are skipped.

// 1. Install dependencies (run in your terminal):
// npm install --save-dev prettier prettier-plugin-vue

// 2. Create a .prettierrc.js file in your project root:
// prettier.config.js
module.exports = {
  vueExcludeBlocks: ['style'], // Instructs prettier-plugin-vue to ignore <style> blocks
  singleQuote: true,
  semi: false,
  trailingComma: 'es5',
  tabWidth: 2,
  printWidth: 100,
};

// 3. Create a sample Vue SFC (e.g., src/components/MyComponent.vue) with some messy formatting:
/*
<template>
  <div class="hello-world">
    <h1>{{ msg }}</h1>
    <p>
      This is a sample component with intentionally
      <span class="highlight">poor formatting</span>
      in its script and style blocks.
    </p>
  </div>
</template>

<script setup>
import {
  ref
} from 'vue';

const msg =
  'Hello Vue 3 + TypeScript';
const count = ref(0); // This line will be formatted
const incrementCount = () => {
  count.value++;
};
</script>

<style scoped>
.hello-world {
  font-family: Arial, sans-serif;
  text-align:center;
  color: #2c3e50;
  margin-top:    60px;
}
.highlight {
  color: #42b983;
  font-weight:  bold;
}
</style>
*/

// 4. Run Prettier from your terminal to format files:
// npx prettier --write "src/**/*.vue"

// This command will format the <template> and <script> blocks of your Vue files
// according to the .prettierrc.js rules. However, due to `vueExcludeBlocks: ['style']`,
// the <style> block's formatting (e.g., extra spaces, irregular indentation) will remain
// untouched by Prettier, demonstrating the plugin's core functionality.