1
---
2
name: cross-platform-compatibility
3
description: >
4
Handle cross-platform compatibility including file paths, environment
5
detection, platform-specific dependencies, and testing across Windows, macOS,
6
and Linux. Use when dealing with platform-specific code or OS compatibility.
7
---
8
9
# Cross-Platform Compatibility
10
11
## Table of Contents
12
13
- [Overview](#overview)
14
- [When to Use](#when-to-use)
15
- [Quick Start](#quick-start)
16
- [Reference Guides](#reference-guides)
17
- [Best Practices](#best-practices)
18
19
## Overview
20
21
Comprehensive guide to writing code that works seamlessly across Windows, macOS, and Linux. Covers file path handling, environment detection, platform-specific features, and testing strategies.
22
23
## When to Use
24
25
- Building applications for multiple operating systems
26
- Handling file system operations
27
- Managing platform-specific dependencies
28
- Detecting operating system and architecture
29
- Working with environment variables
30
- Building cross-platform CLI tools
31
- Dealing with line endings and character encodings
32
- Managing platform-specific build processes
33
34
## Quick Start
35
36
Minimal working example:
37
38
```typescript
39
// ❌ BAD: Hardcoded paths with platform-specific separators
40
const configPath = "C:\\Users\\user\\config.json"; // Windows only
41
const dataPath = "/home/user/data.txt"; // Unix only
42
43
// ✅ GOOD: Use path module
44
import path from "path";
45
import os from "os";
46
47
// Platform-independent path construction
48
const configPath = path.join(os.homedir(), "config", "app.json");
49
const dataPath = path.join(process.cwd(), "data", "users.txt");
50
51
// Resolve relative paths
52
const absolutePath = path.resolve("./config/settings.json");
53
54
// Get path components
55
const dirname = path.dirname("/path/to/file.txt"); // '/path/to'
56
const basename = path.basename("/path/to/file.txt"); // 'file.txt'
57
const extname = path.extname("/path/to/file.txt"); // '.txt'
58
59
// Normalize paths (handle .. and .)
60
const normalized = path.normalize("/path/to/../file.txt"); // '/path/file.txt'
61
```
62
63
## Reference Guides
64
65
Detailed implementations in the `references/` directory:
66
67
| Guide | Contents |
68
|---|---|
69
| [File Path Handling](references/file-path-handling.md) | File Path Handling |
70
| [Platform Detection](references/platform-detection.md) | Platform Detection |
71
| [Line Endings](references/line-endings.md) | Line Endings |
72
| [Environment Variables](references/environment-variables.md) | Environment Variables |
73
| [Shell Commands](references/shell-commands.md) | Shell Commands |
74
| [File Permissions](references/file-permissions.md) | File Permissions |
75
| [Process Management](references/process-management.md) | Process Management |
76
| [Platform-Specific Dependencies](references/platform-specific-dependencies.md) | Platform-Specific Dependencies |
77
| [Testing Across Platforms](references/testing-across-platforms.md) | Testing Across Platforms |
78
| [Character Encoding](references/character-encoding.md) | Character Encoding |
79
| [Build Configuration](references/build-configuration.md) | Build Configuration |
80
81
## Best Practices
82
83
### ✅ DO
84
85
- Use path.join() or path.resolve() for paths
86
- Use os.EOL for line endings
87
- Detect platform at runtime when needed
88
- Test on all target platforms
89
- Use optionalDependencies for platform-specific modules
90
- Handle file permissions gracefully
91
- Use shell escaping for user input
92
- Normalize line endings in text files
93
- Use UTF-8 encoding by default
94
- Document platform-specific behavior
95
- Provide fallbacks for platform-specific features
96
- Use CI/CD to test on multiple platforms
97
98
### ❌ DON'T
99
100
- Hardcode file paths with backslashes or forward slashes
101
- Assume Unix-only features (signals, permissions, symlinks)
102
- Ignore Windows-specific quirks (drive letters, UNC paths)
103
- Use platform-specific commands without fallbacks
104
- Assume case-sensitive file systems
105
- Forget about different line endings
106
- Use platform-specific APIs without checking
107
- Hardcode environment variable access patterns
108
- Ignore character encoding issues
109