Vue SFC Parser
Vue Parser is a utility library designed to parse the contents of Vue Single File Components (.vue files) by leveraging an Abstract Syntax Tree (AST), specifically based on the `parse5` library. It enables developers to extract specific content from `<template>`, `<script>`, or `<style>` tags. A key differentiator is its ability to pad the extracted content with a specified string (defaulting to comments for script/style) to preserve original line numbers, which is particularly beneficial for linting and error reporting tools. The current stable version is 1.1.6, but the package has not seen updates for approximately four years as of April 2026. This lack of recent development suggests the project is abandoned, which may lead to compatibility issues with newer versions of Vue.js (especially Vue 3+), Node.js, or related ecosystem tools.
Common errors
-
TypeError: vueParser.parse is not a function
cause Incorrect import statement or attempting to destructure a default export as a named export when the library provides a default export. This could also happen if the `require` statement is used incorrectly in a CommonJS environment.fixEnsure you are using `import vueParser from 'vue-parser'` for ESM or `const vueParser = require('vue-parser')` for CommonJS to correctly access the default exported function. -
Cannot find module 'vue-parser'
cause The package is not installed or incorrectly referenced in your project's `node_modules`.fixRun `npm install vue-parser` or `yarn add vue-parser` to ensure the package is installed and accessible to your project. -
No content found for tag 'script' with lang 'ts'
cause The `lang` option in the `parse` function call is too restrictive, or the specified tag/language combination does not exist in the Vue SFC. This can also occur if the parser doesn't correctly identify custom `lang` attributes.fixVerify the `lang` attribute in your Vue file's tag (e.g., `<script lang="ts">`) and ensure it's included in the `lang` array option passed to `parse`. For instance, `{ lang: ['ts', 'js'] }` would match both TypeScript and JavaScript scripts.
Warnings
- breaking The package has not been updated for approximately four years and is considered abandoned. It is highly likely to have compatibility issues with newer versions of Vue.js (especially Vue 3+) and modern Node.js environments.
- gotcha The `parse` function, by default, pads the extracted content with `// ` for script/style tags or empty lines for template tags to retain original line numbers. While beneficial for linters, this behavior might be unexpected if raw, unpadded content is desired directly.
- gotcha The parser's reliance on `parse5` for AST generation means it handles HTML-like structures. However, it may not fully understand advanced Vue-specific template directives or new `<script setup>` syntax introduced in Vue 3, potentially leading to incorrect parsing or incomplete extraction for complex SFCs.
Install
-
npm install vue-parser -
yarn add vue-parser -
pnpm add vue-parser
Imports
- vueParser
import { vueParser } from 'vue-parser'import vueParser from 'vue-parser'
- vueParser
const { parse } = require('vue-parser')const vueParser = require('vue-parser') - ParseOptions
import type { ParseOptions } from 'vue-parser'
Quickstart
import vueParser from 'vue-parser';
const vueFileContents = `
<template lang="pug">
.hello
h1 {{ msg }}
</template>
<script lang="js">
export default {
name: 'Hello',
data () {
return {
msg: 'Hello World!'
}
}
}
</script>
<style>
h1 {
font-weight: normal;
}
</style>
`;
console.log('--- Extracting Script Content ---');
const myScriptContents = vueParser.parse(vueFileContents, 'script', { lang: ['js', 'jsx'] });
console.log(myScriptContents);
console.log('\n--- Extracting Template Content ---');
const myTemplateContents = vueParser.parse(vueFileContents, 'template', { lang: ['pug'] });
console.log(myTemplateContents);
console.log('\n--- Extracting Style Content (default CSS) ---');
const myStyleContents = vueParser.parse(vueFileContents, 'style');
console.log(myStyleContents);