{"id":19242,"library":"babel-plugin-version","title":"babel-plugin-version","description":"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.","status":"active","version":"0.2.3","language":"javascript","source_language":"en","source_url":null,"tags":["javascript","babel-plugin","version"],"install":[{"cmd":"npm install babel-plugin-version","lang":"bash","label":"npm"},{"cmd":"yarn add babel-plugin-version","lang":"bash","label":"yarn"},{"cmd":"pnpm add babel-plugin-version","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Works with both ESM and CJS – the CommonJS pattern is also valid.","wrong":"const plugin = require('babel-plugin-version')","symbol":"default import","correct":"import plugin from 'babel-plugin-version'"},{"note":"Babel resolves the short name 'version' automatically.","wrong":"plugins: ['babel-plugin-version']","symbol":"babel.config.js usage","correct":"plugins: ['version']"},{"note":"Options must be in a nested array, not separate items.","wrong":"plugins: ['version', { define: '__PKG_VERSION__' }]","symbol":"configured usage","correct":"plugins: [['version', { define: '__PKG_VERSION__', identifier: false, stringLiteral: true }]]"}],"quickstart":{"code":"// .babelrc or babel.config.js\n{\n  \"plugins\": [\n    \"version\"\n  ]\n}\n\n// Input: const appVersion = __VERSION__;\n// Output: const appVersion = \"0.2.3\";\n// The version is read from the nearest package.json at Babel transform time.","lang":"javascript","description":"A Babel plug-in that replaces __VERSION__ tokens with the version from package.json at build time."},"warnings":[{"fix":"Ensure __VERSION__ is used as a standalone identifier or a string literal (e.g., `const v = __VERSION__;` or `\"__VERSION__\"`).","message":"The 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.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Ensure that Babel runs in the context of your project root where package.json exists.","message":"The 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).","severity":"gotcha","affected_versions":">=0.0.0"},{"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.","message":"The 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.","severity":"deprecated","affected_versions":">=0.2.0"}],"env_vars":null,"last_verified":"2026-04-25T00:00:00.000Z","next_check":"2026-07-24T00:00:00.000Z","problems":[{"fix":"Run: npm install --save-dev babel-plugin-version","cause":"The plugin is not installed as a dev dependency.","error":"Error: Cannot find module 'babel-plugin-version'"},{"fix":"Ensure package.json exists in the Babel working directory and contains a 'version' string.","cause":"The plugin cannot find a 'version' field in package.json.","error":"TypeError: Cannot read property 'replace' of undefined"},{"fix":"Use the correct nested array syntax: ['version', { options }] inside the plugins array.","cause":"The plugin configuration array is incorrectly formatted.","error":"error: unexpected token 'version' in babelrc"}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null}