or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-slash

Convert Windows backslash paths to slash paths

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/slash@5.1.x

To install, run

npx @tessl/cli install tessl/npm-slash@5.1.0

index.mddocs/

Slash

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.

Package Information

  • Package Name: slash
  • Package Type: npm
  • Language: JavaScript/TypeScript
  • Installation: npm install slash

Core Imports

import slash from 'slash';

For CommonJS:

const slash = require('slash');

Basic Usage

import path from 'node:path';
import slash from 'slash';

const windowsPath = path.join('foo', 'bar');
// On Unix    => foo/bar
// On Windows => foo\\bar

const unifiedPath = slash(windowsPath);
// On Unix    => foo/bar
// On Windows => foo/bar (converted from foo\\bar)

console.log(unifiedPath); // Always outputs: foo/bar

Capabilities

Path Conversion

Converts Windows backslash paths to forward slash paths while preserving extended-length paths.

/**
 * Convert Windows backslash paths to slash paths: `foo\\bar` ➔ `foo/bar`.
 * Forward-slash paths can be used in Windows as long as they're not extended-length paths.
 * 
 * @param path - A Windows backslash path.
 * @returns A path with forward slashes.
 */
function slash(path: string): string;

Parameters:

  • path (string): A Windows backslash path to convert

Returns:

  • string: A path with forward slashes

Behavior:

  • Converts all backslashes (\) to forward slashes (/) in the provided path
  • Preserves extended-length paths that start with \\?\ unchanged (Windows-specific extended path format)
  • Handles Unicode characters in paths correctly
  • Works with mixed path separators (e.g., c:/aaaa\\bbbb becomes c:/aaaa/bbbb)

Usage Examples:

import slash from 'slash';

// Basic conversion
slash('c:\\aaaa\\bbbb');
// Returns: 'c:/aaaa/bbbb'

// Mixed separators
slash('c:/aaaa\\bbbb');
// Returns: 'c:/aaaa/bbbb'

// Unicode characters
slash('c:\\aaaa\\bbbb\\★');
// Returns: 'c:/aaaa/bbbb/★'

// Extended-length paths (Windows) - preserved unchanged
slash('\\\\?\\c:\\aaaa\\bbbb');
// Returns: '\\\\?\\c:\\aaaa\\bbbb' (unchanged)

// Already forward slashes - no change needed
slash('c:/aaaa/bbbb');
// Returns: 'c:/aaaa/bbbb'

Common Use Cases:

import path from 'node:path';
import slash from 'slash';

// Consistent paths for build tools
const srcPath = slash(path.join('src', 'components', 'Button.js'));
// Always: 'src/components/Button.js'

// Cross-platform file processing
import fs from 'node:fs';
const files = fs.readdirSync('.')
  .map(file => slash(path.resolve(file)))
  .filter(file => file.endsWith('.js'));

// URL-safe paths for web applications
const webPath = slash(path.relative(process.cwd(), filePath));
// Safe to use in URLs and web contexts