Registry / devops / babel-plugin-version

babel-plugin-version

JSON →
library0.2.3jsnpmunverified

A Babel plugin that replaces occurrences of the string literal or identifier `__VERSION__` with the version string from `package.json` (e.g., `"0.2.3"`). Current stable version 0.2.3 enables compile-time version injection. The plugin can be configured to use a custom define name and to toggle replacement of identifiers vs. string literals. This is a lightweight zero-dependency tool for build processes using Babel for JavaScript/TypeScript projects.

npm install babel-plugin-version
INSTALL
IMPORT
SIG · BABEL-PLUGIN-VERSI
B
babel-plugin-version
devopsjavascriptv0.2.3
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default import
import plugin from 'babel-plugin-version'
const plugin = require('babel-plugin-version')
Works with both ESM and CJS – the CommonJS pattern is also valid.
babel.config.js usage
plugins: ['version']
plugins: ['babel-plugin-version']
Babel resolves the short name 'version' automatically.
configured usage
plugins: [['version', { define: '__PKG_VERSION__', identifier: false, stringLiteral: true }]]
plugins: ['version', { define: '__PKG_VERSION__' }]
Options must be in a nested array, not separate items.

A Babel plug-in that replaces __VERSION__ tokens with the version from package.json at build time.

// .babelrc or babel.config.js { "plugins": [ "version" ] } // Input: const appVersion = __VERSION__; // Output: const appVersion = "0.2.3"; // The version is read from the nearest package.json at Babel transform time.
Debug
Known issues
gotchaThe plugin only replaces `__VERSION__` (or custom define) when it appears as an identifier or string literal. It does NOT handle dynamic code or computed expressions.
fix
Ensure __VERSION__ is used as a standalone identifier or a string literal (e.g., `const v = __VERSION__;` or `"__VERSION__"`).
affects: >=0.0.0
gotchaThe plugin reads the version from the nearest package.json file at compile time. If the working directory does not contain a package.json, the version will be undefined or empty string (depending on implementation).
fix
Ensure that Babel runs in the context of your project root where package.json exists.
affects: >=0.0.0
deprecatedThe plugin is still functional but deprecated in favor of using environment variables (e.g., process.env.npm_package_version) or define plugin (e.g., @rollup/plugin-replace) in modern builds.
fix
Consider migrating to @rollup/plugin-replace for Rollup, or use DefinePlugin for webpack, or simply read process.env.npm_package_version at runtime if version is needed.
affects: >=0.2.0
Errors
Common errors & fixes
Error: Cannot find module 'babel-plugin-version'
The plugin is not installed as a dev dependency.
fix
Run: npm install --save-dev babel-plugin-version
TypeError: Cannot read property 'replace' of undefined
The plugin cannot find a 'version' field in package.json.
fix
Ensure package.json exists in the Babel working directory and contains a 'version' string.
error: unexpected token 'version' in babelrc
The plugin configuration array is incorrectly formatted.
fix
Use the correct nested array syntax: ['version', { options }] inside the plugins array.
Upgrade
Version history
0.2.3latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
6 hits · last 30 days
node
6
Resources
babel-plugin-version — npm install babel-plugin-version · libregistry