Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

mkver

photostructure1.2kMIT4.0.2TypeScript support: included

Node.js access to your app's version and release metadata

Version

readme

Easy access to your version and build metadata from within Node.js

npm version Node.js CI CodeQL

Why?

Simple, reliable access to version and build information from within Node.js and Electron apps should be easy, without runtime dependencies.

Even if you push git SHAs into your package.json, after minification, asarification, and installation into platform-specific directory structures, you'll still be fighting __dirname bugs trying to find where your package.json went.

In TypeScript and ES6 module environments, there's a simple, minification-compatible and asar-compatible solution for importing information from outside your current file.

It's called import. Or for CommonJS users, require.

By writing build-specific information as constants in code within our codebase, consuming this metadata becomes trivial. Add it to your build pipeline, import it, and focus on the big problems.

What?

mkver produces either:

Each file contains your git SHA and version information exported as constants.

Example output

// Version.ts

export const version = "1.2.3-beta.4"
export const versionMajor = 1
export const versionMinor = 2
export const versionPatch = 3
export const versionPrerelease = ["beta", 4]
export const release = "1.2.3-beta.4+20220101105815"
export const gitSha = "dc336bc8e1ea6b4e2f393f98233839b6c23cb812"
export const gitDate = new Date(1641063495000)
export default {
  version,
  versionMajor,
  versionMinor,
  versionPatch,
  versionPrerelease,
  release,
  gitSha,
  gitDate,
}

The filename can be anything you want, but the file extension must be .ts, .mjs, .js, or .cjs.

It also creates a SemVer-compatible release tag in the format ${version}+${YYYYMMDDhhmmss of gitDate}, and a gitDate Date instance representing when the last git commit occurred.

Module Format

mkver itself is distributed as a CommonJS package to ensure maximum compatibility across different Node.js environments and platforms. While the tool internally uses ES modules during development, the distributed package uses CommonJS to avoid compatibility issues that can arise with ESM on certain platforms (particularly Windows).

However, mkver generates output files in whatever format you need:

  • TypeScript (.ts) with ES module exports
  • ES modules (.mjs) with ES module exports
  • CommonJS (.js or .cjs) with CommonJS exports

The output format is determined solely by the file extension you specify.

Installation

Step 1: Add mkver to your package.json

npm i --save-dev mkver

Step 2: For TypeScript users

Add a pre... npm script to your package.json that runs mkver:

  "scripts": {
    ...
    "precompile": "mkver",
    "compile": "tsc",
    ...
  }

Step 2: For JavaScript module or CommonJS users

Add mkver as a pre... script for your test script and/or build pipeline in your package.json:

  "scripts": {
    ...
    "prebuild": "mkver ./lib/version.mjs", // or ./lib/version.js or ./lib/version.cjs
    "build": "webpack", // or whatever you use
    ...
  }

Step 3: Add to .gitignore

You should add your Version.ts, version.mjs, version.js, or version.cjs file to your project's .gitignore.

How

mkver is a simple, dependency-free, three-step tool:

  1. mkver recursively searches for a package.json starting from the current directory and extracts the version value.
  2. mkver executes git rev-parse HEAD to get the last commit SHA. Git must be available in your PATH.
  3. mkver writes the output to the specified file (default: ./Version.ts). The file extension determines the output format (TypeScript, ESM, or CommonJS). Existing files will be overwritten.

If anything goes wrong, mkver will output errors to stderr and exit with a non-zero code.

Use with TypeScript or MJS modules

import { version, release } from "./Version"

Use with CommonJS

const { version, release } = require("./version") // Ensure the case matches your mkver output filename

Remember to specify mkver version.js (or version.cjs) in your npm script (see Installation Step 2 above).

Bash access to your version info

Need access to your release from a bash deploy script?

  # For CommonJS (.js or .cjs files):
  release=$(node -e "console.log(require('./path/to/version.js').release)")

  # For ESM (.mjs or .ts files):
  release=$(node -e "import('./path/to/version.mjs').then(m => console.log(m.release))")

Changelog

See CHANGELOG.md.

changelog

Changelog

This module follows semver.

The MAJOR or API version is incremented for

  • 💔 Non-backwards-compatible API changes

The MINOR or UPDATE version is incremented for

  • ✨ Backwards-compatible features

The PATCH version is incremented for

  • 🐞 Backwards-compatible bug fixes
  • 📦 Minor packaging changes

v4.0.2

  • ✨ Add ESLint configuration for JavaScript and TypeScript linting
  • 📦 Fix cross-platform TypeScript compilation in tests using npx
  • 📦 Update GitHub Actions workflows to use numeric Node.js versions

v4.0.1

  • 💔 Drop Node.js v18 and v21 support

  • ✨ Add .cjs file extension support for explicit CommonJS output

  • 📦 add Node.js v22, v23, and v24 support
  • 📦 Switch CI workflow from yarn to npm
  • 🐞 Enhance error handling for file creation and TypeScript compilation in tests
  • 🐞 Fix Windows test compatibility issues (reverted ESM migration due to test breakage)

v3.0.2

  • 🐞 Remove console.log with headSha metadata

v3.0.1

  • 🐞 Fix mkver shebang

v3.0.0

  • 💔 Drop support for obsolete versions of Node.js

  • ✨ Support non-CLI programmatic mkver calls. Include typings.

  • 📦 Merge code from bin/mkver.js into mkver.ts

  • 📦 Replace sync calls with async calls

  • 📦 Added eslint.

  • 📦 Added prettier and import reordering.

  • 📦 Upgrade all dev dependencies.

v2.1.0

  • 📦 Upgrade all dev dependencies.

  • 📦 Remove unused internal map function

v2.0.0

  • ✨ If the version is parseable by semver, mkver will export versionMajor, versionMinor, versionPatch, and versionPrerelease fields. Examples are now in the README.

  • 📦 Improved test coverage with several different version flavors (including prerelease suffixes)

  • 📦 Upgrade all dev dependencies.

  • 💔/📦 Dropped Node 12 and added Node 18 to the CI matrix (12 is EOL)

v1.6.0

  • 📦 Upgrade all dev dependencies

v1.5.0

  • 📦 Add default exports (so import v from "./Versions" works, if you want namespaced access).
  • 📦 Upgrade all dev dependencies

v1.4.0

  • ✨ Support ECMAScript module formats (.mjs)
  • 📦 Upgrade all dev dependencies

v1.3.6

  • 📦 Upgrade all dev dependencies (including TypeScript 4.1)

v1.3.5

  • 📦 Upgrade all dev dependencies

v1.3.4

  • 📦 Upgrade all dev dependencies

v1.3.3

  • 📦 Version.js and Version.ts both use semicolons now
  • 📦 version.ts has test coverage
  • 📦 Upgrade all dev dependencies
  • 📦 Prettier 2.0.0 diffs

v1.3.2

  • 📦 Upgrade all dev dependencies, migrate mocha opts

v1.3.1

  • 📦 Upgrade all dev dependencies

v1.3.0

  • ✨ Support arbitrarily deep subpackages (if running on node 10.13+)
  • 📦 Add better integration tests (by spawning the binfile)
  • 📦 prettier .js
  • 📦 Upgrade all dev dependencies

v1.2.0

  • ✨ Remove runtime dependency on fs-extra
  • 📦 Upgrade all dev dependencies

v1.1.2

  • 📦 Support for mkver --help

v1.1.1

  • 📦 Upgrade all dev dependencies
  • 📦 Add node 11 to the build matrix

v1.1.0

  • ✨ The release suffix is now the YYYYMMDDhhmmss for better human readability. The base64 of the minute unixtime was, uh, "clever," but I kept wanting to know when the build was, and I don't (yet) think in b64.
  • 📦 Upgrade all dev deps, including TS 3.0.3.

v1.0.0

  • ✨ release values use the git commit date rather than the SHA, so the same version will have monotonically increasing releases for subsequent commits.

v0.0.4

  • 📦 Publish on linux to chmod bin/mkver

v0.0.2

  • 📦 Whitespace between comment and exports

v0.0.1

  • 🎉 First release