Vite Plugin Package Config
raw JSON → 0.1.1 verified Mon Apr 27 auth: no javascript
A Vite plugin (v0.1.1, latest) that allows extending Vite configuration via a `vite` field in package.json. Developed by Anthony Fu, it provides a declarative, JSON-based alternative to vite.config.ts for static config like resolve aliases and build output, making it easier to share and manipulate config across tools. It requires Vite ^2.0.0, ships TypeScript types, and has a minimal API — just one function call.
Common errors
error Cannot find module 'vite-plugin-package-config' ↓
cause Package not installed or missing from node_modules.
fix
Run 'npm install -D vite-plugin-package-config'.
error TypeError: PkgConfig is not a function ↓
cause Named import used instead of default import.
fix
Use 'import PkgConfig from "vite-plugin-package-config"'.
error Invalid config: field 'vite' in package.json is not recognized ↓
cause The custom 'vite' field is not part of Vite's official schema but is read by the plugin.
fix
Ignore the warning; the plugin parses it correctly.
Warnings
gotcha Only static config that can be JSON-serialized is supported. Functions, objects with circular references, or dynamic values cannot be placed in package.json. ↓
fix Use vite.config.ts for dynamic configurations.
gotcha The plugin merges config from package.json with vite.config.ts; duplicate config fields may cause unexpected behavior depending on merge order. ↓
fix Explicitly set precedence by ordering plugins or using the 'enforce' property.
deprecated The 'output' alias field (e.g., 'build.output') is deprecated in Vite; use 'outDir' instead. ↓
fix Use Vite's official config fields like 'outDir' instead of 'output'.
gotcha The plugin reads package.json from the project root; if the root is not correctly configured, it may fail silently. ↓
fix Ensure process.cwd() matches the directory containing package.json, or set root in Vite config.
Install
npm install vite-plugin-package-config yarn add vite-plugin-package-config pnpm add vite-plugin-package-config Imports
- PkgConfig wrong
const PkgConfig = require('vite-plugin-package-config')correctimport PkgConfig from 'vite-plugin-package-config' - default wrong
import { PkgConfig } from 'vite-plugin-package-config'correctimport PkgConfig from 'vite-plugin-package-config' - type definitions wrong
import { VitePluginPackageConfig } from 'vite-plugin-package-config'correctimport type { Plugin } from 'vite'; import PkgConfig from 'vite-plugin-package-config'
Quickstart
// vite.config.ts
import PkgConfig from 'vite-plugin-package-config';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
PkgConfig()
]
});
// package.json
{
"name": "my-app",
"version": "1.0.0",
"vite": {
"resolve": {
"alias": {
"@": "/src"
}
},
"build": {
"outDir": "dist"
}
}
}