Registry / devops / vite-plugin-git-commit-hash

vite-plugin-git-commit-hash

JSON →
library1.0.7jsnpmunverified

A Vite plugin that injects the latest Git commit hash as a global `GIT_COMMIT_HASH` variable at build time. Current stable version is 1.0.7, released under MIT license. It is lightweight, has zero runtime dependencies (only peer dependency on Vite >=3), and ships TypeScript declarations. The plugin is ESM and CommonJS compatible. Key differentiator: it provides a simple, declarative way to expose git hash without complex build scripts or environment variables.

npm install vite-plugin-git-commit-hash
INSTALL
IMPORT
SIG · VITE-PLUGIN-GIT-CO
V
vite-plugin-git-commit-hash
devopsjavascriptv1.0.7
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 18223 runs
build_error
glibc
node 18223 runs
build_error
Code
Verified usage

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

gitCommitHashPlugin
import { gitCommitHashPlugin } from 'vite-plugin-git-commit-hash'
import gitCommitHashPlugin from 'vite-plugin-git-commit-hash'
This plugin exports a named function, not a default export.
GIT_COMMIT_HASH
console.log(GIT_COMMIT_HASH); // global variable
import { GIT_COMMIT_HASH } from 'vite-plugin-git-commit-hash'
GIT_COMMIT_HASH is a global variable defined at build time, not an imported symbol.
type GIT_COMMIT_HASH
declare const GIT_COMMIT_HASH: string; // in a .d.ts file
import type { GIT_COMMIT_HASH } from 'vite-plugin-git-commit-hash'
The plugin does not export any types; you must declare the global in a declaration file.

Shows how to add the plugin to Vite config and use the global GIT_COMMIT_HASH variable in your code.

// vite.config.ts import { defineConfig } from 'vite'; import { gitCommitHashPlugin } from 'vite-plugin-git-commit-hash'; export default defineConfig({ plugins: [gitCommitHashPlugin()], }); // main.ts console.log(`App version: ${GIT_COMMIT_HASH}`);
Debug
Known issues
breakingPlugin uses Vite's define() which replaces global identifiers literally. If your code uses a variable named GIT_COMMIT_HASH for another purpose, it will be overwritten.
fix
Avoid using GIT_COMMIT_HASH as a variable name elsewhere, or use a different config option (if available).
affects: all
gotchaGIT_COMMIT_HASH is only available after build, not during development server (HMR) unless manually configured.
fix
Check process.env.NODE_ENV or use Vite's import.meta.env.DEV to conditionally access the hash.
affects: >=1.0.0
gotchaThe plugin requires Vite >=3. Older Vite versions will cause errors.
fix
Upgrade Vite to version 3 or higher.
affects: all
Errors
Common errors & fixes
Cannot find name 'GIT_COMMIT_HASH'. Do you need to add a type declaration?
The global variable GIT_COMMIT_HASH is not recognized by TypeScript without a declaration.
fix
Add `declare const GIT_COMMIT_HASH: string;` to a .d.ts file (e.g., env.d.ts).
GIT_COMMIT_HASH is not defined
The plugin may not be running or the variable is not replaced correctly in development mode.
fix
Ensure the plugin is added to the Vite config's plugins array. In development, consider that GIT_COMMIT_HASH is replaced only after build.
Upgrade
Version history
1.0.7latest on npm
Audit
Dependencies
viterequiredRequired as a peer dependency to function as a Vite plugin.
Agent activity
2 hits · last 30 days
node
2
Resources
vite-plugin-git-commit-hash — npm install vite-plugin-git-commit-hash · libregistry