Command Line Interface
Complete reference for VitePress CLI commands and options.
Installation
Global Installation
bash
npm install -g vitepressLocal Installation
bash
npm install -D vitepressCommands
vitepress dev
Start the development server.
bash
vitepress dev [root]Arguments:
root- Path to the docs directory (default: current directory)
Options:
--port <port>- Specify port (default: 5173)--host <host>- Specify hostname (default: localhost)--https- Use HTTPS--open- Open browser on server start--base <path>- Public base path--outDir <dir>- Output directory for build--cache <dir>- Cache directory--logLevel <level>- Log level (info | warn | error | silent)
Examples:
bash
# Start dev server on default port
vitepress dev
# Start on specific port
vitepress dev --port 3000
# Start with HTTPS
vitepress dev --https
# Specify docs directory
vitepress dev docs
# Open browser automatically
vitepress dev --openvitepress build
Build the site for production.
bash
vitepress build [root]Arguments:
root- Path to the docs directory (default: current directory)
Options:
--outDir <dir>- Output directory (default: .vitepress/dist)--base <path>- Public base path--target <target>- Build target (default: modules)--minify [minifier]- Enable/disable minification--sourcemap- Generate source maps--ssr- Build for server-side rendering--mpa- Build in MPA mode
Examples:
bash
# Basic build
vitepress build
# Build to custom directory
vitepress build --outDir dist
# Build with source maps
vitepress build --sourcemap
# Build for SSR
vitepress build --ssrvitepress preview
Preview the production build locally.
bash
vitepress preview [root]Arguments:
root- Path to the docs directory (default: current directory)
Options:
--port <port>- Specify port (default: 4173)--host <host>- Specify hostname--https- Use HTTPS--open- Open browser on server start--outDir <dir>- Directory to serve (default: .vitepress/dist)
Examples:
bash
# Preview on default port
vitepress preview
# Preview on specific port
vitepress preview --port 8080
# Preview with HTTPS
vitepress preview --httpsvitepress init
Initialize a new VitePress project.
bash
vitepress init [project-name]Arguments:
project-name- Name of the project directory
Options:
--template <template>- Use a specific template--git- Initialize git repository--install- Install dependencies automatically
Examples:
bash
# Initialize in current directory
vitepress init
# Initialize new project
vitepress init my-docs
# Initialize with template
vitepress init --template blog
# Initialize with git and auto-install
vitepress init my-docs --git --installConfiguration
Config File Detection
VitePress automatically detects configuration files in this order:
.vitepress/config.js.vitepress/config.ts.vitepress/config.mjs.vitepress/config.mts
Environment Variables
Development:
NODE_ENV=development- Set automatically in dev modeVITEPRESS_DEV=true- Set automatically in dev mode
Build:
NODE_ENV=production- Set automatically in build modeVITEPRESS_BUILD=true- Set automatically in build mode
Custom Variables:
bash
# Set custom base path
BASE_PATH=/docs vitepress build
# Set custom port
PORT=3000 vitepress dev
# Enable debug mode
DEBUG=vitepress:* vitepress devAdvanced Usage
Custom Scripts
Add scripts to your package.json:
json
{
"scripts": {
"docs:dev": "vitepress dev docs",
"docs:build": "vitepress build docs",
"docs:preview": "vitepress preview docs",
"docs:clean": "rm -rf docs/.vitepress/dist docs/.vitepress/cache"
}
}CI/CD Integration
GitHub Actions:
yaml
name: Deploy VitePress site to Pages
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
cache: npm
- run: npm ci
- run: npm run docs:build
- uses: actions/deploy-pages@v2
with:
artifact_name: github-pages
path: docs/.vitepress/distNetlify:
toml
# netlify.toml
[build]
command = "npm run docs:build"
publish = "docs/.vitepress/dist"
[build.environment]
NODE_VERSION = "18"Vercel:
json
{
"buildCommand": "npm run docs:build",
"outputDirectory": "docs/.vitepress/dist",
"installCommand": "npm install"
}Docker Integration
Dockerfile:
dockerfile
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run docs:build
FROM nginx:alpine
COPY --from=0 /app/docs/.vitepress/dist /usr/share/nginx/html
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]Docker Compose:
yaml
version: '3.8'
services:
vitepress:
build: .
ports:
- "80:80"
volumes:
- ./docs:/app/docs
environment:
- NODE_ENV=productionDebugging
Debug Mode
Enable debug logging:
bash
# Debug all VitePress internals
DEBUG=vitepress:* vitepress dev
# Debug specific modules
DEBUG=vitepress:config,vitepress:build vitepress build
# Debug with verbose output
vitepress dev --logLevel infoCommon Issues
Port Already in Use:
bash
# Find process using port
lsof -ti:5173
# Kill process
kill -9 $(lsof -ti:5173)
# Use different port
vitepress dev --port 3000Build Failures:
bash
# Clear cache
rm -rf node_modules/.vite docs/.vitepress/cache
# Reinstall dependencies
npm ci
# Build with verbose logging
vitepress build --logLevel infoMemory Issues:
bash
# Increase Node.js memory limit
NODE_OPTIONS="--max-old-space-size=4096" vitepress build
# Use alternative build approach
vitepress build --target esnextPerformance Optimization
Build Optimization
bash
# Enable minification
vitepress build --minify
# Disable source maps for production
vitepress build --no-sourcemap
# Use specific target for better performance
vitepress build --target es2020Development Optimization
bash
# Disable host check for faster startup
vitepress dev --host 0.0.0.0
# Use specific cache directory
vitepress dev --cache .cache
# Reduce log output
vitepress dev --logLevel warnProgrammatic API
Using VitePress Programmatically
javascript
import { createServer, build, preview } from 'vitepress'
// Development server
const server = await createServer('docs', {
port: 3000,
host: 'localhost'
})
await server.listen()
console.log('Server running at http://localhost:3000')
// Build
await build('docs', {
outDir: 'dist',
base: '/my-site/',
minify: true
})
// Preview
const previewServer = await preview('docs', {
port: 4000,
outDir: 'dist'
})Custom Build Process
javascript
import { defineConfig } from 'vitepress'
import { spawn } from 'child_process'
export default defineConfig({
// ... config
buildEnd: async (siteConfig) => {
// Custom post-build processing
console.log('Build completed, running custom tasks...')
// Generate sitemap
await generateSitemap(siteConfig)
// Optimize images
await optimizeImages()
// Deploy to CDN
await deployToCDN()
}
})
async function generateSitemap(config) {
// Custom sitemap generation logic
}
async function optimizeImages() {
return new Promise((resolve) => {
const child = spawn('imagemin', ['dist/**/*.{jpg,png}', '--out-dir=dist'])
child.on('close', resolve)
})
}Troubleshooting
Common Error Messages
"Cannot resolve module":
bash
# Clear node_modules and reinstall
rm -rf node_modules package-lock.json
npm install"Port 5173 is already in use":
bash
# Use different port
vitepress dev --port 3000
# Or kill existing process
npx kill-port 5173"Build failed with exit code 1":
bash
# Check for syntax errors in config
node -c .vitepress/config.js
# Build with verbose logging
vitepress build --logLevel info"Out of memory" errors:
bash
# Increase memory limit
export NODE_OPTIONS="--max-old-space-size=8192"
vitepress buildGetting Help
Check Version:
bash
vitepress --versionView Help:
bash
vitepress --help
vitepress dev --help
vitepress build --helpCommunity Resources:
Migration
From VuePress 1.x
bash
# Install migration tool
npm install -g vitepress-migration-tool
# Run migration
vitepress-migrate ./docs
# Manual adjustments may be neededFrom Other SSGs
bash
# From GitBook
gitbook-to-vitepress ./gitbook-docs ./vitepress-docs
# From Docusaurus
docusaurus-to-vitepress ./docusaurus-docs ./vitepress-docsBest Practices
Development Workflow
- Use
npm scriptsfor consistent commands - Set up proper
.gitignorefor cache and build files - Use environment variables for configuration
- Implement proper error handling in build scripts
Production Deployment
- Always run
vitepress buildbefore deployment - Use CDN for static assets
- Implement proper caching headers
- Monitor build performance and optimize as needed
Performance Monitoring
- Use build time analysis tools
- Monitor bundle size
- Implement performance budgets
- Regular dependency updates