babel-plugin-version-transform
raw JSON → 1.0.0 verified Sat Apr 25 auth: no javascript deprecated
A Babel plugin (v1.0.0, stable, low release cadence) that replaces the identifier VERSION with the stringified version number from package.json during compilation. This is a specialized transform plugin that enables compile-time injection of package version without runtime overhead. Unlike environment variable approaches, this plugin works at the AST level within Babel's plugin pipeline. It is unmaintained since 2017 and relies on Babel 6 APIs, making it incompatible with Babel 7+.
Common errors
error Error: Plugin/preset files are not allowed to export objects, only functions. ↓
cause Using the plugin with Babel 7+ which expects function-form plugins.
fix
Upgrade to a Babel 7 compatible version of the plugin or switch to '@babel/plugin-transform-version'.
error ReferenceError: VERSION is not defined ↓
cause The plugin did not replace VERSION because it was not executed (e.g., missing plugin configuration).
fix
Ensure the plugin is listed in .babelrc or the transform call, and that the file is processed by Babel.
error SyntaxError: Unexpected token ↓
cause Using the plugin with unsupported syntax (e.g., JSX, TypeScript) without the appropriate Babel presets.
fix
Add the corresponding Babel presets (e.g., '@babel/preset-react' for JSX) to your Babel configuration.
Warnings
deprecated Plugin only works with Babel 6; not compatible with Babel 7+ ↓
fix Use a modern alternative like '@babel/plugin-transform-version' or inline replacement with code transformation.
gotcha The plugin only replaces the bare identifier VERSION, not string literals or computed expressions. ↓
fix Ensure VERSION is used as a standalone identifier, not inside strings or as part of larger expressions.
gotcha VERSION is a global replacement; any identifier named VERSION in scope will be replaced. ↓
fix Avoid using the variable name VERSION for other purposes in files where the plugin is active.
Install
npm install babel-plugin-version-transform yarn add babel-plugin-version-transform pnpm add babel-plugin-version-transform Imports
- default (plugin) wrong
plugins: [require('babel-plugin-version-transform')]correctplugins: ['version-transform']
Quickstart
// .babelrc
{
"plugins": ["version-transform"]
}
// source.js
import pkg from './package.json';
function log(data) {
xhr({...data, version: VERSION});
}