2023-10-03 18:54:51 +02:00
# @push.rocks/smartenv
2024-04-14 17:32:19 +02:00
2025-11-01 15:26:26 +00:00
> 🚀 **Universal JavaScript Runtime Detection** - One library for Node.js, Deno, Bun, and Browser
2020-06-25 22:26:04 +00:00
2025-11-01 15:26:26 +00:00
A powerful TypeScript library that provides comprehensive runtime environment detection and safe module loading across **all major JavaScript runtimes ** . Write once, run everywhere with confidence.
## Why smartenv?
Modern JavaScript runs in many environments - Node.js, Deno, Bun, and browsers. Writing isomorphic code that works everywhere is challenging. **smartenv ** solves this by providing:
2024-04-14 17:32:19 +02:00
2025-11-01 15:26:26 +00:00
- ✅ **Accurate runtime detection ** - Distinguishes Node.js from Deno, Bun, and browsers without false positives
- ✅ **Smart module loading ** - Load the right modules for each runtime automatically
- ✅ **Platform detection ** - Detect macOS, Linux, Windows, and CI environments
- ✅ **Zero dependencies ** (except @push .rocks/smartpromise)
- ✅ **Full TypeScript support ** with complete type definitions
- ✅ **Battle-tested ** - Comprehensive test suite across all runtimes
## Install
2024-04-14 17:32:19 +02:00
```bash
2025-07-28 12:00:51 +00:00
pnpm install @push .rocks/smartenv --save
2024-04-14 17:32:19 +02:00
```
2020-06-25 22:26:04 +00:00
2025-11-01 15:26:26 +00:00
```bash
npm install @push .rocks/smartenv --save
```
## Quick Start
```typescript
import { Smartenv } from '@push .rocks/smartenv';
const env = new Smartenv();
2020-06-25 22:26:04 +00:00
2025-11-01 15:26:26 +00:00
// Detect the runtime
console.log(env.runtimeEnv); // 'node' | 'deno' | 'bun' | 'browser'
2024-04-14 17:32:19 +02:00
2025-11-01 15:26:26 +00:00
// Load modules safely for your runtime
const pathModule = await env.getSafeModuleFor('server', 'path');
if (pathModule) {
console.log('Path module loaded!');
}
// Check specific runtimes
if (env.isNode) {
console.log(`Running on Node.js ${env.nodeVersion}` );
} else if (env.isDeno) {
console.log(`Running on Deno ${env.denoVersion}` );
} else if (env.isBun) {
console.log(`Running on Bun ${env.bunVersion}` );
} else {
console.log(`Running in ${env.userAgent}` );
}
```
## Core Features
### 🎯 Multi-Runtime Detection
2025-07-28 12:00:51 +00:00
2025-11-01 15:26:26 +00:00
Accurately detects all major JavaScript runtimes using proper detection order to avoid false positives:
2024-04-14 17:32:19 +02:00
```typescript
2025-11-01 15:26:26 +00:00
const env = new Smartenv();
// Runtime type - returns one of: 'node' | 'deno' | 'bun' | 'browser'
console.log(env.runtimeEnv);
// Boolean checks for each runtime
env.isNode; // true only in Node.js
env.isDeno; // true only in Deno
env.isBun; // true only in Bun
env.isBrowser; // true only in browser
2024-04-14 17:32:19 +02:00
```
2025-11-01 15:26:26 +00:00
**Why detection order matters:** Deno and Bun provide `process` objects for Node.js compatibility. smartenv checks for `Deno` and `Bun` globals first, then `process.versions.node` , ensuring accurate detection.
### 📦 Smart Module Loading
2025-07-28 12:00:51 +00:00
2025-11-01 15:26:26 +00:00
The new `getSafeModuleFor()` API lets you target specific runtimes or groups:
2024-04-14 17:32:19 +02:00
```typescript
2025-11-01 15:26:26 +00:00
// Load for a specific runtime
const fsNode = await env.getSafeModuleFor('node', 'fs');
// Load for Deno (requires 'node:' prefix for Node.js built-ins)
const fsDeno = await env.getSafeModuleFor('deno', 'node:path');
// Load for all server-side runtimes (Node.js + Deno + Bun)
const pathModule = await env.getSafeModuleFor('server', 'path');
// Load for multiple specific runtimes
const crypto = await env.getSafeModuleFor(['node', 'bun'], 'crypto');
// Browser module loading
const jQuery = await env.getSafeModuleFor(
'browser',
'https://code.jquery.com/jquery-3.6.0.min.js',
() => window.jQuery
);
2024-04-14 17:32:19 +02:00
```
2025-11-01 15:26:26 +00:00
**Target options:**
- `'node'` - Node.js only
- `'deno'` - Deno only
- `'bun'` - Bun only
- `'browser'` - Browser only
- `'server'` - Shorthand for `['node', 'deno', 'bun']`
- `['node', 'deno']` - Array of specific runtimes
If the current runtime doesn't match the target, the method returns `undefined` and logs a warning.
### 🖥️ Platform Detection
Detect operating systems in server-side runtimes (Node.js, Deno, Bun):
```typescript
const env = new Smartenv();
// Async OS detection
const isMac = await env.isMacAsync(); // macOS
const isLinux = await env.isLinuxAsync(); // Linux
const isWindows = await env.isWindowsAsync(); // Windows
if (isMac) {
console.log('Running on macOS');
}
```
### 🔢 Version Information
Get version strings for each runtime:
```typescript
const env = new Smartenv();
2025-07-28 12:00:51 +00:00
2025-11-01 15:26:26 +00:00
// Returns version string or 'undefined' if not in that runtime
env.nodeVersion; // e.g., 'v20.10.0'
env.denoVersion; // e.g., '1.40.0'
env.bunVersion; // e.g., '1.0.20'
env.userAgent; // Browser user agent string
```
### 🏗️ CI Detection
Detect if running in a continuous integration environment:
```typescript
const env = new Smartenv();
if (env.isCI) {
console.log('Running in CI environment');
// Enable extended test suite, different build config, etc.
}
```
2025-07-28 12:00:51 +00:00
## API Reference
2025-11-01 15:26:26 +00:00
### Runtime Detection Properties
#### `runtimeEnv: TRuntimeType`
Returns the detected runtime as a string.
**Type:** `'node' | 'deno' | 'bun' | 'browser'`
```typescript
const env = new Smartenv();
console.log(env.runtimeEnv); // 'node', 'deno', 'bun', or 'browser'
```
2025-07-28 12:00:51 +00:00
#### `isNode: boolean`
2025-11-01 15:26:26 +00:00
`true` if running in Node.js (specifically checks for `process.versions.node` ).
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
if (env.isNode) {
console.log('Node.js environment');
2025-07-28 12:00:51 +00:00
}
```
2025-11-01 15:26:26 +00:00
#### `isDeno: boolean`
`true` if running in Deno (checks for `Deno` global).
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
if (env.isDeno) {
console.log('Deno environment');
2025-07-28 12:00:51 +00:00
}
```
2025-11-01 15:26:26 +00:00
#### `isBun: boolean`
`true` if running in Bun (checks for `Bun` global).
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
if (env.isBun) {
console.log('Bun environment');
}
2025-07-28 12:00:51 +00:00
```
2025-11-01 15:26:26 +00:00
#### `isBrowser: boolean`
`true` if running in a browser environment.
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
if (env.isBrowser) {
console.log('Browser environment');
2025-07-28 12:00:51 +00:00
}
```
2025-11-01 15:26:26 +00:00
#### `isCI: boolean`
2025-07-28 12:00:51 +00:00
2025-11-01 15:26:26 +00:00
`true` if running in a CI environment (checks `process.env.CI` in server runtimes).
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
if (env.isCI) {
// CI-specific logic
2025-07-28 12:00:51 +00:00
}
```
2025-11-01 15:26:26 +00:00
### Version Properties
#### `nodeVersion: string`
Node.js version string. Returns `'undefined'` if not in Node.js.
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
console.log(env.nodeVersion); // 'v20.10.0'
```
#### `denoVersion: string`
Deno version string. Returns `'undefined'` if not in Deno.
```typescript
console.log(env.denoVersion); // '1.40.0'
```
#### `bunVersion: string`
Bun version string. Returns `'undefined'` if not in Bun.
```typescript
console.log(env.bunVersion); // '1.0.20'
```
#### `userAgent: string`
Browser user agent string. Returns `'undefined'` if not in browser.
```typescript
console.log(env.userAgent); // 'Mozilla/5.0...'
```
### Platform Detection Methods
#### `isMacAsync(): Promise<boolean>`
Asynchronously checks if running on macOS. Works in Node.js, Deno, and Bun.
```typescript
const isMac = await env.isMacAsync();
if (isMac) {
console.log('Running on macOS');
2025-07-28 12:00:51 +00:00
}
```
#### `isLinuxAsync(): Promise<boolean>`
2025-11-01 15:26:26 +00:00
Asynchronously checks if running on Linux. Works in Node.js, Deno, and Bun.
2024-04-14 17:32:19 +02:00
```typescript
2025-11-01 15:26:26 +00:00
const isLinux = await env.isLinuxAsync();
2025-07-28 12:00:51 +00:00
if (isLinux) {
console.log('Running on Linux');
}
```
2025-11-01 15:26:26 +00:00
#### `isWindowsAsync(): Promise<boolean>`
2024-04-14 17:32:19 +02:00
2025-11-01 15:26:26 +00:00
Asynchronously checks if running on Windows. Works in Node.js, Deno, and Bun.
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
const isWindows = await env.isWindowsAsync();
if (isWindows) {
console.log('Running on Windows');
2025-07-28 12:00:51 +00:00
}
2024-04-14 17:32:19 +02:00
```
2025-11-01 15:26:26 +00:00
### Module Loading Methods
2024-04-14 17:32:19 +02:00
2025-11-01 15:26:26 +00:00
#### `getSafeModuleFor<T>(target, moduleNameOrUrl, getFunction?): Promise<T | undefined>`
2024-04-14 17:32:19 +02:00
2025-11-01 15:26:26 +00:00
**The recommended way to load modules** - supports runtime targeting with flexible options.
2024-04-14 17:32:19 +02:00
2025-11-01 15:26:26 +00:00
**Parameters:**
- `target: TRuntimeTarget | TRuntimeTarget[]` - Runtime(s) to load for
- `moduleNameOrUrl: string` - Module name (server runtimes) or URL (browser)
- `getFunction?: () => any` - Function to retrieve module (required for browser)
**Returns:** `Promise<T | undefined>` - Loaded module or undefined if runtime doesn't match
**Examples:**
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
// Node.js only
const fs = await env.getSafeModuleFor('node', 'fs');
// Deno only (note: use 'node:' prefix for Node.js built-ins)
const path = await env.getSafeModuleFor('deno', 'node:path');
// All server runtimes
const crypto = await env.getSafeModuleFor('server', 'crypto');
// Multiple specific runtimes
const util = await env.getSafeModuleFor(['node', 'bun'], 'util');
// Browser
const lib = await env.getSafeModuleFor(
'browser',
'https://cdn.example.com/lib.js',
() => window.MyLib
);
2024-04-14 17:32:19 +02:00
```
2025-11-01 15:26:26 +00:00
#### `getSafeNodeModule<T>(moduleName, runAfterFunc?): Promise<T>`
**Legacy method** - Loads modules in server-side runtimes (Node.js, Deno, Bun).
2024-04-14 17:32:19 +02:00
```typescript
2025-11-01 15:26:26 +00:00
const fs = await env.getSafeNodeModule('fs');
2025-07-28 12:00:51 +00:00
2025-11-01 15:26:26 +00:00
// With post-load callback
const express = await env.getSafeNodeModule('express', async (mod) => {
console.log('Express loaded');
2025-07-28 12:00:51 +00:00
});
2024-04-14 17:32:19 +02:00
```
2025-11-01 15:26:26 +00:00
#### `getSafeWebModule(url, getFunction): Promise<any>`
**Legacy method** - Loads web modules via script tag in browser. Prevents duplicate loading.
2024-04-14 17:32:19 +02:00
```typescript
2025-11-01 15:26:26 +00:00
const jQuery = await env.getSafeWebModule(
2025-07-28 12:00:51 +00:00
'https://code.jquery.com/jquery-3.6.0.min.js',
() => window.jQuery
);
2024-04-14 17:32:19 +02:00
```
2025-11-01 15:26:26 +00:00
#### `getEnvAwareModule(options): Promise<any>`
2025-07-28 12:00:51 +00:00
2025-11-01 15:26:26 +00:00
**Legacy method** - Loads environment-appropriate modules.
2024-04-14 17:32:19 +02:00
```typescript
2025-11-01 15:26:26 +00:00
const module = await env.getEnvAwareModule({
nodeModuleName: 'node-fetch',
webUrlArg: 'https://unpkg.com/whatwg-fetch@3 .6.2/dist/fetch.umd.js',
getFunction: () => window.fetch
});
2025-07-28 12:00:51 +00:00
```
2025-11-01 15:26:26 +00:00
### Utility Methods
#### `printEnv(): Promise<void>`
2025-07-28 12:00:51 +00:00
2025-11-01 15:26:26 +00:00
Prints environment information to console for debugging.
2024-04-14 17:32:19 +02:00
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
await env.printEnv();
// Node.js: "running on NODE" + version
// Deno: "running on DENO" + version
// Bun: "running on BUN" + version
// Browser: "running on BROWSER" + user agent
2025-07-28 12:00:51 +00:00
```
2025-11-01 15:26:26 +00:00
## Real-World Examples
### 🌍 Isomorphic Cryptography
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
import { Smartenv } from '@push .rocks/smartenv';
const env = new Smartenv();
// Load crypto for any server runtime
const crypto = await env.getSafeModuleFor('server', 'crypto');
if (crypto) {
const hash = crypto.createHash('sha256');
hash.update('hello world');
console.log(hash.digest('hex'));
} else if (env.isBrowser) {
// Use Web Crypto API
const encoder = new TextEncoder();
const data = encoder.encode('hello world');
const hashBuffer = await crypto.subtle.digest('SHA-256', data);
console.log(Array.from(new Uint8Array(hashBuffer))
.map(b => b.toString(16).padStart(2, '0'))
.join(''));
2025-07-28 12:00:51 +00:00
}
2024-04-14 17:32:19 +02:00
```
2025-11-01 15:26:26 +00:00
### 📁 Cross-Runtime File System
2024-04-14 17:32:19 +02:00
```typescript
2025-11-01 15:26:26 +00:00
const env = new Smartenv();
async function readConfig() {
if (env.isNode) {
const fs = await env.getSafeModuleFor('node', 'fs/promises');
return JSON.parse(await fs.readFile('config.json', 'utf-8'));
} else if (env.isDeno) {
const content = await Deno.readTextFile('config.json');
return JSON.parse(content);
} else if (env.isBun) {
const file = Bun.file('config.json');
return await file.json();
} else {
// Browser: fetch from server
const response = await fetch('/config.json');
return response.json();
}
2025-07-28 12:00:51 +00:00
}
2024-04-14 17:32:19 +02:00
```
2025-11-01 15:26:26 +00:00
### 🔧 Development vs Production
2024-04-14 17:32:19 +02:00
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
const env = new Smartenv();
async function setupEnvironment() {
// Different behavior in CI
if (env.isCI) {
console.log('CI Environment detected');
// Skip interactive prompts, use default values
return { mode: 'ci', verbose: true };
}
// Platform-specific paths
if (await env.isMacAsync()) {
return { configPath: '/Users/username/.config' };
} else if (await env.isLinuxAsync()) {
return { configPath: '/home/username/.config' };
} else if (await env.isWindowsAsync()) {
return { configPath: 'C:\\Users\\username\\AppData\\Local' };
}
}
```
### 🎨 Conditional Analytics Loading
```typescript
const env = new Smartenv();
async function initializeAnalytics() {
if (!env.isBrowser) {
console.log('Analytics skipped - not in browser');
return;
}
// Only load analytics in browser
const analytics = await env.getSafeModuleFor(
'browser',
'https://www.googletagmanager.com/gtag/js?id=GA_MEASUREMENT_ID',
() => window.gtag
);
if (analytics) {
analytics('config', 'GA_MEASUREMENT_ID');
}
}
```
### 🧪 Runtime-Specific Testing
```typescript
const env = new Smartenv();
async function runTests() {
console.log(`Testing in ${env.runtimeEnv}` );
// Load test utilities for current runtime
const testLib = await env.getSafeModuleFor(
['node', 'deno', 'bun'],
'@git .zone/tstest'
2025-07-28 12:00:51 +00:00
);
2025-11-01 15:26:26 +00:00
if (testLib) {
// Run server-side tests
await runServerTests(testLib);
} else {
// Run browser tests
await runBrowserTests();
}
2025-07-28 12:00:51 +00:00
}
```
2024-04-14 17:32:19 +02:00
2025-07-28 12:00:51 +00:00
## TypeScript Support
2024-04-14 17:32:19 +02:00
2025-11-01 15:26:26 +00:00
smartenv is written in TypeScript and provides complete type definitions:
2025-07-28 12:00:51 +00:00
```typescript
2025-11-01 15:26:26 +00:00
import {
Smartenv,
TRuntimeType, // 'node' | 'deno' | 'bun' | 'browser'
TRuntimeTarget, // TRuntimeType | 'server'
IEnvObject
} from '@push .rocks/smartenv';
const env: Smartenv = new Smartenv();
const runtime: TRuntimeType = env.runtimeEnv;
// Type-safe module loading
const fs = await env.getSafeModuleFor<typeof import('fs')>('node', 'fs');
if (fs) {
fs.readFileSync('./file.txt', 'utf-8'); // Full type support
2025-07-28 12:00:51 +00:00
}
```
2024-04-14 17:32:19 +02:00
2025-11-01 15:26:26 +00:00
## How Runtime Detection Works
smartenv uses a careful detection order to avoid false positives:
1. **Check for Deno ** - `globalThis.Deno?.version` (Deno has `process` for compatibility)
2. **Check for Bun ** - `globalThis.Bun?.version` (Bun also has `process` )
3. **Check for Node.js ** - `globalThis.process?.versions?.node` (must be specific)
4. **Check for Browser ** - `globalThis.window && globalThis.document` (fallback)
This order is critical because Deno and Bun provide `process` objects for Node.js compatibility, which would cause false Node.js detection if checked first.
## Migration from 4.x to 5.x
**Breaking Changes:**
1. * * `runtimeEnv` return type changed:**
- **Before:** `'node' | 'browser'`
- **After:** `'node' | 'deno' | 'bun' | 'browser'`
2. * * `isNode` is now more specific:**
- **Before:** Returns `true` for Node.js, Deno, and Bun
- **After:** Returns `true` only for Node.js
**Migration guide:**
```typescript
// Before (4.x)
if (env.isNode) {
// This ran in Node.js, Deno, and Bun
}
// After (5.x) - Option 1: Check all server runtimes
if (env.isNode || env.isDeno || env.isBun) {
// Works in Node.js, Deno, and Bun
}
// After (5.x) - Option 2: Use the new module loading API
const module = await env.getSafeModuleFor('server', 'path');
if (module) {
// Module loaded successfully in any server runtime
}
```
## Performance
- ⚡ **Lightweight ** - Minimal overhead with lazy evaluation
- 🚀 **Fast detection ** - Simple boolean checks, no heavy operations
- 💾 **Cached results ** - Detection runs once, results are cached
- 📦 **Small bundle ** - ~5KB minified, tree-shakeable
## Browser Compatibility
Tested and working in:
- ✅ Chrome/Chromium (latest)
- ✅ Firefox (latest)
- ✅ Safari (latest)
- ✅ Edge (latest)
## Node.js Compatibility
- ✅ Node.js 18.x
- ✅ Node.js 20.x (LTS)
- ✅ Node.js 22.x
## Deno Compatibility
- ✅ Deno 1.40+
**Note:** When using Deno, use the `node:` prefix for Node.js built-in modules:
```typescript
const path = await env.getSafeModuleFor('deno', 'node:path');
```
## Bun Compatibility
- ✅ Bun 1.0+
2024-04-14 17:32:19 +02:00
## License and Legal Information
2025-11-01 15:26:26 +00:00
This repository contains open-source code that is licensed under the MIT License. A copy of the MIT License can be found in the [license ](license ) file within this repository.
2024-04-14 17:32:19 +02:00
**Please note:** The MIT License does not grant permission to use the trade names, trademarks, service marks, or product names of the project, except as required for reasonable and customary use in describing the origin of the work and reproducing the content of the NOTICE file.
### Trademarks
This project is owned and maintained by Task Venture Capital GmbH. The names and logos associated with Task Venture Capital GmbH and any related products or services are trademarks of Task Venture Capital GmbH and are not included within the scope of the MIT license granted herein. Use of these trademarks must comply with Task Venture Capital GmbH's Trademark Guidelines, and any usage must be approved in writing by Task Venture Capital GmbH.
2020-06-25 22:26:04 +00:00
2024-04-14 17:32:19 +02:00
### Company Information
2020-06-25 22:26:04 +00:00
2025-11-01 15:26:26 +00:00
Task Venture Capital GmbH
2024-04-14 17:32:19 +02:00
Registered at District court Bremen HRB 35230 HB, Germany
2020-06-25 22:26:04 +00:00
2024-04-14 17:32:19 +02:00
For any legal inquiries or if you require further information, please contact us via email at hello@task .vc.
2020-06-25 22:26:04 +00:00
2024-04-14 17:32:19 +02:00
By using this repository, you acknowledge that you have read this section, agree to comply with its terms, and understand that the licensing of the code does not imply endorsement by Task Venture Capital GmbH of any derivative works.