Webpack Version File Plugin
This Webpack plugin, `webpack-version-file`, generates a file (typically `version.txt`) containing crucial project metadata such as the package name, version number, and build date during the Webpack compilation process. It is particularly useful for tracking which version of an application is deployed in various environments, aiding in debugging and support. The plugin, currently at version `0.1.7` (last updated in 2017), is considered abandoned. It utilizes EJS for templating, allowing extensive customization of the output file's content using data from `package.json`, predefined variables like `buildDate`, and custom data passed via its options. Its simplicity and direct integration into the Webpack build lifecycle were its key differentiators, though its lack of maintenance is a significant concern for modern Webpack and Node.js environments.
Common errors
-
TypeError: Class constructor VersionFile cannot be invoked without 'new'
cause Attempting to call the plugin constructor `VersionFile()` directly without the `new` keyword.fixEnsure you instantiate the plugin using `new VersionFile()` within your Webpack plugins array in `webpack.config.js`. -
Module not found: Error: Can't resolve 'webpack-version-file' in '...'
cause The `webpack-version-file` package is not installed as a development dependency, or Webpack cannot locate it in `node_modules`.fixRun `npm install --save-dev webpack-version-file` or `yarn add --dev webpack-version-file` in your project's root directory to install the package. -
Error: ENOENT: no such file or directory, open '...' (for 'package' or 'template' option)
cause The path specified for the `package` or `template` option in the plugin configuration is incorrect, or the file does not exist at the given path.fixVerify that the file paths provided to the `package` and `template` options are correct and accessible. It's best practice to use `path.resolve(__dirname, 'file.json')` for robust, absolute path resolution.
Warnings
- gotcha The 'webpack-version-file' package has not been updated in over seven years (last release 0.1.7 in 2017) and is considered abandoned. It may not be compatible with recent Webpack versions (v5+) or modern Node.js environments, and will not receive security patches or bug fixes.
- gotcha This plugin is a CommonJS module and uses `require()`. Directly importing it using ES Modules `import` syntax in a pure ESM environment may lead to errors without proper Webpack configuration (e.g., using `resolve.fallback` for Node.js built-ins) or a CommonJS wrapper.
- gotcha The plugin relies on EJS templating. Ensure template syntax is correct, and all variables referenced (e.g., `<%= variable %>`) are either derived from `package.json`, predefined (`buildDate`), or explicitly passed via the `data` option. Unresolved variables will render as empty or undefined in the output.
Install
-
npm install webpack-version-file -
yarn add webpack-version-file -
pnpm add webpack-version-file
Imports
- VersionFile
import VersionFile from 'webpack-version-file';
const VersionFile = require('webpack-version-file'); - new VersionFile()
VersionFile();
new VersionFile({ output: './build/version.txt' })
Quickstart
const path = require('path');
const VersionFile = require('webpack-version-file');
module.exports = {
entry: './src/index.js', // Your application's entry point
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'bundle.js',
},
plugins: [
new VersionFile({
output: path.resolve(__dirname, 'dist', 'version.txt'),
package: path.resolve(__dirname, 'package.json'),
template: './version.ejs', // Path to your custom EJS template file
data: {
buildUser: process.env.USER ?? 'anonymous',
gitCommit: process.env.GITHUB_SHA ?? 'N/A'
},
verbose: true,
})
],
mode: 'development' // or 'production'
};
// To run this example, create a file named 'version.ejs' in your project root:
// <%= name %>@<%= version %>
// Built by: <%= buildUser %>
// Commit: <%= gitCommit %>
// Build date: <%= buildDate %>
// License: <%= license %>
// Environment: <%= process.env.NODE_ENV ?? 'development' %>