Vue Docgen CLI
vue-docgen-cli is a command-line interface tool designed to generate comprehensive Markdown documentation files directly from Vue.js component source code. Built upon the powerful vue-docgen-api, it parses Vue 2 and Vue 3 components (including Composition API with script setup, defineProps, defineEmits, and defineSlots) and extracts their props, events, methods, slots, and other metadata into structured Markdown. The current stable version is 4.79.0, with frequent patch and minor releases often synchronized with updates to vue-docgen-api to maintain compatibility with the latest Vue features. Key differentiators include its highly customizable templating system for output Markdown, support for a configurable `docgen.config.js` file, a watch mode for automatic regeneration, and the ability to integrate with existing build configurations (e.g., Webpack aliases) to correctly resolve component dependencies. It provides a static documentation generation alternative to in-browser style guides.
Common errors
-
Error: Minimum Node.js version 16.3 is required. You are using Node.js X.Y.Z.
cause Running vue-docgen-cli on a Node.js version older than 16.3.fixUpgrade your Node.js environment to version 16.3 or newer. Use 'nvm install 16 && nvm use 16' or similar for your nvm setup. -
Failed to parse the Props passed to Macro function as Type alias reference
cause A bug in vue-docgen-api prevented correct parsing of Type alias references within macro functions.fixUpdate vue-docgen-cli to version 4.79.1 or newer, which includes a fix for this parsing issue. -
TypeError: Cannot read properties of undefined (reading 'extension')
cause A bug related to parsing interface extensions in Vue components.fixUpdate vue-docgen-api to version 4.79.2 or newer, which contains a fix for interface extension parsing. -
Error: No component files found for glob: src/components/**/*.vue
cause The glob pattern provided to vue-docgen-cli did not match any existing component files, or the `componentsRoot` was misconfigured.fixVerify that your `componentsRoot` and `components` glob pattern in `docgen.config.js` (or command-line arguments) accurately reflect the location and naming convention of your Vue components. Ensure the paths are correct relative to your project root.
Warnings
- breaking vue-docgen-cli requires Node.js version 16.3 or higher. Running on older Node.js environments will result in errors.
- gotcha Older versions of vue-docgen-cli (and its core dependency vue-docgen-api) may fail to correctly parse newer Vue 3.3+ syntax, specifically `defineEmits` syntax introduced in Vue 3.3. This was addressed in vue-docgen-api@4.75.0 and vue-docgen-cli@4.75.0.
- gotcha The `docgen.config.js` file is processed as a CommonJS module. Using ES Module syntax (`import`/`export`) directly within this configuration file will cause errors unless your project is explicitly configured to handle `.js` files as ES Modules or you rename the config file to `docgen.config.mjs`.
- gotcha Alias resolution (e.g., `@components`) defined in bundlers like Webpack or Vite is not automatically picked up by vue-docgen-cli. This can lead to issues where component imports within your Vue files cannot be resolved during documentation generation, affecting type inference or linking.
Install
-
npm install vue-docgen-cli -
yarn add vue-docgen-cli -
pnpm add vue-docgen-cli
Imports
- DocgenCLIConfig
import { DocgenCLIConfig } from 'vue-docgen-cli';import type { DocgenCLIConfig } from 'vue-docgen-cli'; - generate
const generate = require('vue-docgen-cli').generate;import { generate } from 'vue-docgen-cli'; - componentTemplate
import componentTemplate from 'vue-docgen-cli/dist/templates/component';
import { componentTemplate } from 'vue-docgen-cli/dist/templates';
Quickstart
```javascript
// package.json
{
"name": "my-vue-project-docs",
"version": "1.0.0",
"description": "Generate docs for Vue components",
"scripts": {
"docs:generate": "vue-docgen src/components/**/*.vue docs/components",
"docs:watch": "vue-docgen -w src/components/**/*.vue docs/components"
},
"devDependencies": {
"vue-docgen-cli": "^4.79.0"
}
}
// src/components/MyButton.vue
<template>
<button @click="handleClick">{{ label }}</button>
</template>
<script setup lang="ts">
/**
* A universal button component.
* @displayName MyButton
*/
import { ref } from 'vue';
/**
* Props for MyButton.
*/
interface MyButtonProps {
/**
* The text label for the button.
* @type {string}
* @example 'Click Me'
*/
label: string;
/**
* Determines if the button is disabled.
*/
disabled?: boolean;
}
const props = withDefaults(defineProps<MyButtonProps>(), {
disabled: false,
});
const emit = defineEmits<{ (e: 'click', event: MouseEvent): void }>();
const count = ref(0);
/**
* Handles the click event on the button.
* @param {MouseEvent} event - The native mouse event.
* @event click
*/
const handleClick = (event: MouseEvent) => {
if (!props.disabled) {
count.value++;
emit('click', event);
}
};
</script>
// Terminal commands
$ npm install
$ npm run docs:generate
```