A lightweight TypeScript package for tracking application versions with environment awareness.
- π Automatic version detection from package.json
- π Smart environment detection
- β‘ Zero configuration needed
- π Environment variable support
- π Beautiful console logging
- π TypeScript support with full type definitions
- π¦ Zero dependencies
- π§ͺ Fully tested
# Using npm
npm install env-version-log
# Using yarn
yarn add env-version-log
# Using pnpm
pnpm add env-version-logimport { VersionTracker } from 'env-version-log';
// Initialize with automatic detection
const tracker = await VersionTracker.initialize();
// Log version info
tracker.logVersionInfo();The package automatically detects:
-
App Name & Version
- Reads from your package.json (configurable path)
- No configuration needed
- Falls back to environment variables if needed
-
Environment
- Smart detection based on:
- Environment variables (NODE_ENV)
- Build-time environment (Vite's MODE)
- Runtime context (hostname, URL patterns)
- Common patterns:
- Development: localhost, 127.0.0.1
- Staging: URLs containing 'staging' or 'test'
- Production: URLs containing 'prod' or 'production'
- Smart detection based on:
You can override the automatic detection using environment variables. The package checks for variables in the following order:
namein package.jsonAPP_NAMEin env file- Default: "Unknown App"
versionin package.jsonAPP_VERSIONin env file- Default: "0.0.0"
NODE_ENVin env fileimport.meta.env.NODE_ENV(Vite)- Default: "development"
{
"name": "my-app",
"version": "1.0.0"
}APP_NAME=my-app
APP_VERSION=1.0.0
NODE_ENV=development// vite.config.ts
import { defineConfig } from 'vite';
import pkg from './package.json';
export default defineConfig({
define: {
'import.meta.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV || 'development'),
},
});Note: The package will first try to read from package.json, then fall back to environment variables if package.json is not available or doesn't contain the required values.
For browser environments (like React/Vite apps), you must use one of these methods:
-
Vite Config (Recommended)
// vite.config.ts import { defineConfig } from 'vite'; import pkg from './package.json'; export default defineConfig({ define: { 'import.meta.env.VITE_APP_NAME': JSON.stringify(pkg.name), 'import.meta.env.VITE_APP_VERSION': JSON.stringify(pkg.version), }, });
-
.env File
VITE_APP_NAME=my-app VITE_APP_VERSION=1.0.0
Then initialize the tracker without specifying packageJsonPath:
// Initialize version tracker
const initVersionTracker = async () => {
const tracker = await VersionTracker.initialize();
tracker.logVersionInfo();
};Note: In browser environments, the package will use environment variables instead of trying to read
package.json. ThepackageJsonPathoption is only used in Node.js environments.
If you're seeing "Unknown App" or "0.0.0" version, check your browser's console for debug messages. The package will log:
- Whether it's running in a browser environment
- Available environment variables
- Values found in
import.meta.env
This will help you identify why the values aren't being picked up correctly.
Creates a new VersionTracker instance with automatic detection.
// Automatic detection
const tracker = await VersionTracker.initialize();
// With custom config
const tracker = await VersionTracker.initialize({
appName: 'Custom App',
version: '2.0.0',
environment: 'staging'
});
// With custom package.json path
const tracker = await VersionTracker.initialize({
packageJsonPath: './custom/path/package.json'
});Returns the current version information.
const info = tracker.getVersionInfo();
// {
// appName: 'my-app',
// version: '1.0.0',
// environment: 'development',
// lastUpdated: '2024-03-20T12:00:00.000Z'
// }Logs the current version information in a beautiful format.
tracker.logVersionInfo();
// π¦ Application Information:
// βββββββββββββββββββββββββββββββββββββββ
// π App Name: my-app
// π§ Environment: development
// π’ Version: 1.0.0
// β° Last Updated: 3/20/2024, 12:00:00 PM
// βββββββββββββββββββββββββββββββββββββββChecks for updates from environment variables.
const hasUpdates = await tracker.checkForUpdates();Gets the application version from package.json or environment variables.
// Default package.json path
const version = await getAppVersion();
// Custom package.json path
const version = await getAppVersion('./custom/path/package.json');Gets the application name from package.json or environment variables.
// Default package.json path
const name = await getAppName();
// Custom package.json path
const name = await getAppName('./custom/path/package.json');Works in both Node.js and browser environments:
- Node.js: Uses process.env
- Browser: Uses import.meta.env (Vite) or process.env (Create React App)
- Automatic environment detection based on hostname and URL patterns
Contributions are welcome! Please feel free to submit a Pull Request.
MIT Β© BattaTech
If you find this package helpful, please consider giving it a βοΈ on GitHub!