vite-plugin-package-version
raw JSON → 1.1.0 verified Mon Apr 27 auth: no javascript
Vite plugin that injects the version from package.json into the Vite environment as import.meta.env.PACKAGE_VERSION. Version 1.1.0 is the current stable release; it requires Vite >=2.0.0-beta.69. The plugin is lightweight and focused solely on exposing the package version. Unlike rollup plugins or manual env variable injection, it provides a simple, zero-config approach for Vite projects. No breaking changes reported; TypeScript types are included.
Common errors
error Error: Cannot find module 'vite-plugin-package-version' ↓
cause Package not installed or incorrect import path.
fix
Run 'npm install vite-plugin-package-version' and ensure import uses default style.
error import.meta.env.PACKAGE_VERSION is undefined ↓
cause Plugin not added to Vite config or version() not called.
fix
Add version() to plugins array in vite.config.js.
error TypeError: Cannot read properties of undefined (reading 'PACKAGE_VERSION') ↓
cause Accessing import.meta.env before plugin initialization.
fix
Move access inside a function or component that runs after Vite resolves.
error require() of ES Module not supported ↓
cause Using CommonJS require() on an ESM-only package.
fix
Use import syntax or set type: 'module' in package.json.
Warnings
gotcha Plugin only works with Vite >=2.0.0-beta.69. Older versions are not supported. ↓
fix Upgrade Vite to version 2.0.0-beta.69 or later.
gotcha The injected variable is only available at runtime via import.meta.env.PACKAGE_VERSION. It will not be available in Node.js scripts or during build-time config. ↓
fix Use dynamic import or build-time replacement if needed outside Vite environment.
gotcha import.meta.env.PACKAGE_VERSION is a string. Using it in numeric operations may cause NaN. ↓
fix Parse with Number() or use string methods as appropriate.
gotcha The plugin expects the project's package.json at the project root. If placed elsewhere, the version may be incorrect. ↓
fix Ensure package.json is in the root directory of the Vite project.
Install
npm install vite-plugin-package-version yarn add vite-plugin-package-version pnpm add vite-plugin-package-version Imports
- default wrong
const version = require('vite-plugin-package-version')correctimport version from 'vite-plugin-package-version' - version wrong
import { version } from 'vite-plugin-package-version'correctimport version from 'vite-plugin-package-version' - PACKAGE_VERSION wrong
process.env.PACKAGE_VERSIONcorrectconst pkgVersion = import.meta.env.PACKAGE_VERSION
Quickstart
// vite.config.js
import { defineConfig } from 'vite';
import version from 'vite-plugin-package-version';
export default defineConfig({
plugins: [version()],
});
// In your application code (e.g., main.ts):
const appVersion = import.meta.env.PACKAGE_VERSION;
console.log('App version:', appVersion);