0
# Platform Detection
1
2
Operating system and platform identification with boolean flags for common platforms and Node.js version detection.
3
4
## Capabilities
5
6
### Platform String
7
8
Raw platform string from the underlying process or runtime.
9
10
```typescript { .api }
11
/**
12
* Value of process.platform or empty string if not available
13
* Common values: 'win32', 'darwin', 'linux', 'freebsd', etc.
14
*/
15
const platform: string;
16
```
17
18
**Usage Examples:**
19
20
```typescript
21
import { platform } from "std-env";
22
23
console.log(`Running on platform: ${platform}`);
24
25
// Switch based on platform
26
switch (platform) {
27
case "win32":
28
console.log("Windows platform");
29
break;
30
case "darwin":
31
console.log("macOS platform");
32
break;
33
case "linux":
34
console.log("Linux platform");
35
break;
36
default:
37
console.log("Unknown platform");
38
}
39
```
40
41
### Windows Detection
42
43
Detects Windows operating system based on platform string.
44
45
```typescript { .api }
46
/**
47
* Detects if running on Windows platform
48
* Based on platform string matching /^win/i pattern
49
*/
50
const isWindows: boolean;
51
```
52
53
**Usage Examples:**
54
55
```typescript
56
import { isWindows } from "std-env";
57
58
if (isWindows) {
59
// Use Windows-specific paths
60
const configPath = path.join(process.env.APPDATA, "myapp");
61
// Handle Windows line endings
62
const content = data.replace(/\n/g, "\r\n");
63
}
64
```
65
66
### Linux Detection
67
68
Detects Linux operating system based on platform string.
69
70
```typescript { .api }
71
/**
72
* Detects if running on Linux platform
73
* Based on platform string matching /^linux/i pattern
74
*/
75
const isLinux: boolean;
76
```
77
78
**Usage Examples:**
79
80
```typescript
81
import { isLinux } from "std-env";
82
83
if (isLinux) {
84
// Use Linux-specific system calls
85
// Check for systemd
86
// Use XDG directories
87
}
88
```
89
90
### macOS Detection
91
92
Detects macOS (Darwin kernel) operating system based on platform string.
93
94
```typescript { .api }
95
/**
96
* Detects if running on macOS (Darwin kernel)
97
* Based on platform string matching /^darwin/i pattern
98
*/
99
const isMacOS: boolean;
100
```
101
102
**Usage Examples:**
103
104
```typescript
105
import { isMacOS } from "std-env";
106
107
if (isMacOS) {
108
// Use macOS-specific paths
109
const configPath = path.join(os.homedir(), "Library", "Application Support");
110
// Handle macOS keychain
111
// Use Cocoa APIs through Node.js bindings
112
}
113
```
114
115
### Node.js Version Detection
116
117
Node.js version information with parsed major version number.
118
119
```typescript { .api }
120
/**
121
* Node.js version string without 'v' prefix, or null if not Node.js
122
* Example: "18.17.0" for Node.js v18.17.0
123
*/
124
const nodeVersion: string | null;
125
126
/**
127
* Node.js major version number, or null if not Node.js
128
* Example: 18 for Node.js v18.17.0
129
*/
130
const nodeMajorVersion: number | null;
131
```
132
133
**Usage Examples:**
134
135
```typescript
136
import { nodeVersion, nodeMajorVersion } from "std-env";
137
138
if (nodeVersion) {
139
console.log(`Node.js version: ${nodeVersion}`);
140
141
if (nodeMajorVersion && nodeMajorVersion >= 18) {
142
// Use Node.js 18+ features
143
// Native fetch API available
144
} else if (nodeMajorVersion && nodeMajorVersion >= 16) {
145
// Use Node.js 16+ features
146
// Stable timers/promises
147
}
148
}
149
150
// Version-specific feature detection
151
if (nodeMajorVersion && nodeMajorVersion >= 20) {
152
// Use Node.js 20+ features like test runner
153
} else {
154
// Fallback to external test framework
155
}
156
```
157
158
## Platform-Specific Usage Patterns
159
160
### Cross-Platform Path Handling
161
162
```typescript
163
import { isWindows, platform } from "std-env";
164
import path from "path";
165
166
// Platform-aware path construction
167
const configDir = isWindows
168
? path.join(process.env.APPDATA || "", "myapp")
169
: path.join(process.env.HOME || "", ".config", "myapp");
170
171
// Platform-specific executable extensions
172
const executableName = isWindows ? "myapp.exe" : "myapp";
173
```
174
175
### Platform-Specific Features
176
177
```typescript
178
import { isLinux, isMacOS, isWindows } from "std-env";
179
180
// Platform-specific notifications
181
if (isLinux) {
182
// Use libnotify
183
} else if (isMacOS) {
184
// Use macOS notification center
185
} else if (isWindows) {
186
// Use Windows toast notifications
187
}
188
```
189
190
### Node.js Version Compatibility
191
192
```typescript
193
import { nodeMajorVersion } from "std-env";
194
195
// Feature detection based on Node.js version
196
const features = {
197
fetch: nodeMajorVersion && nodeMajorVersion >= 18,
198
testRunner: nodeMajorVersion && nodeMajorVersion >= 20,
199
asyncIterators: nodeMajorVersion && nodeMajorVersion >= 10,
200
};
201
202
if (features.fetch) {
203
// Use native fetch
204
const response = await fetch(url);
205
} else {
206
// Use polyfill or alternative
207
const response = await require('node-fetch')(url);
208
}
209
```