0
# Slash
1
2
Slash is a lightweight utility function that converts Windows backslash paths to forward slash paths, enabling cross-platform path compatibility in Node.js applications. It handles the inconsistency where Node.js path methods output backslash paths on Windows, but forward slashes can be used universally across platforms.
3
4
## Package Information
5
6
- **Package Name**: slash
7
- **Package Type**: npm
8
- **Language**: JavaScript/TypeScript
9
- **Installation**: `npm install slash`
10
11
## Core Imports
12
13
```javascript
14
import slash from 'slash';
15
```
16
17
For CommonJS:
18
19
```javascript
20
const slash = require('slash');
21
```
22
23
## Basic Usage
24
25
```javascript
26
import path from 'node:path';
27
import slash from 'slash';
28
29
const windowsPath = path.join('foo', 'bar');
30
// On Unix => foo/bar
31
// On Windows => foo\\bar
32
33
const unifiedPath = slash(windowsPath);
34
// On Unix => foo/bar
35
// On Windows => foo/bar (converted from foo\\bar)
36
37
console.log(unifiedPath); // Always outputs: foo/bar
38
```
39
40
## Capabilities
41
42
### Path Conversion
43
44
Converts Windows backslash paths to forward slash paths while preserving extended-length paths.
45
46
```typescript { .api }
47
/**
48
* Convert Windows backslash paths to slash paths: `foo\\bar` ➔ `foo/bar`.
49
* Forward-slash paths can be used in Windows as long as they're not extended-length paths.
50
*
51
* @param path - A Windows backslash path.
52
* @returns A path with forward slashes.
53
*/
54
function slash(path: string): string;
55
```
56
57
**Parameters:**
58
- `path` (string): A Windows backslash path to convert
59
60
**Returns:**
61
- `string`: A path with forward slashes
62
63
**Behavior:**
64
- Converts all backslashes (`\`) to forward slashes (`/`) in the provided path
65
- Preserves extended-length paths that start with `\\?\` unchanged (Windows-specific extended path format)
66
- Handles Unicode characters in paths correctly
67
- Works with mixed path separators (e.g., `c:/aaaa\\bbbb` becomes `c:/aaaa/bbbb`)
68
69
**Usage Examples:**
70
71
```javascript
72
import slash from 'slash';
73
74
// Basic conversion
75
slash('c:\\aaaa\\bbbb');
76
// Returns: 'c:/aaaa/bbbb'
77
78
// Mixed separators
79
slash('c:/aaaa\\bbbb');
80
// Returns: 'c:/aaaa/bbbb'
81
82
// Unicode characters
83
slash('c:\\aaaa\\bbbb\\★');
84
// Returns: 'c:/aaaa/bbbb/★'
85
86
// Extended-length paths (Windows) - preserved unchanged
87
slash('\\\\?\\c:\\aaaa\\bbbb');
88
// Returns: '\\\\?\\c:\\aaaa\\bbbb' (unchanged)
89
90
// Already forward slashes - no change needed
91
slash('c:/aaaa/bbbb');
92
// Returns: 'c:/aaaa/bbbb'
93
```
94
95
**Common Use Cases:**
96
97
```javascript
98
import path from 'node:path';
99
import slash from 'slash';
100
101
// Consistent paths for build tools
102
const srcPath = slash(path.join('src', 'components', 'Button.js'));
103
// Always: 'src/components/Button.js'
104
105
// Cross-platform file processing
106
import fs from 'node:fs';
107
const files = fs.readdirSync('.')
108
.map(file => slash(path.resolve(file)))
109
.filter(file => file.endsWith('.js'));
110
111
// URL-safe paths for web applications
112
const webPath = slash(path.relative(process.cwd(), filePath));
113
// Safe to use in URLs and web contexts
114
```