normalize-path

Normalize Windows and POSIX file path slashes to unix-style forward slashes

Library
npm
v3.0.0
112stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
33/100Needs Attention
Development Activity0
Maintenance0
Community60
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
59/100Fair
Architecture40
Code Quality70
Innovation35
Learning Curve90

normalize-path is a tiny, single-function utility that converts backslashes to forward slashes, condenses repeated slashes, and strips trailing slashes (optionally) in a file path string. It correctly handles Windows UNC and long-path prefixes so paths remain valid after normalization on path.parse(). Because consistent, cross-platform path strings are a recurring need in build tools and file watchers, it’s a foundational dependency used internally by projects like chokidar and anymatch, and by extension a large share of the Node.js file-watching and glob-matching ecosystem.

What You Get

  • A single normalizePath(path, stripTrailing) function with no dependencies
  • Conversion of Windows backslashes to forward slashes while preserving win32 UNC/long-path prefixes correctly
  • Automatic condensing of repeated slashes into one
  • Optional trailing-slash stripping via the second stripTrailing argument (defaults to stripping)

Common Use Cases

  • Normalizing file paths inside file-watching libraries (e.g. chokidar) so glob patterns match consistently across platforms
  • Ensuring cache keys or generated identifiers derived from file paths are identical on Windows and POSIX systems
  • Sanitizing user- or config-supplied path strings before passing them to glob-matching libraries like anymatch or micromatch
  • Preprocessing paths in build tools and bundlers before comparing or storing them

Under The Hood

Architecture - The entire library is one exported function in index.js (about 30 lines): it type-checks the input, short-circuits for the root path (/ or \) and single-character paths, detects the win32 UNC/long-path prefix pattern (\\?\ or \\.\) and preserves it as //, then splits the remaining path on any run of / or \ via a single regex, optionally drops a trailing empty segment, and rejoins with /. Tech Stack - Plain, dependency-free CommonJS JavaScript with no build step; bower.json indicates it once also targeted Bower-based front-end distribution alongside npm. Code Quality - test.js (87 lines) exercises root paths, repeated slashes, Windows-style paths, UNC/long-path prefixes, and the stripTrailing flag using Mocha/assert-style tests; the function throws a TypeError for non-string input, giving a clear contract despite the tiny surface area. API Design - A single function with a positional boolean second argument (stripTrailing, default true) keeps the call site terse; this is also its main ergonomic limitation for callers who want to combine it with other flags, though its narrow single-purpose scope is a deliberate design choice reflected in its README.md positioning as a focused building block for other tools like chokidar and anymatch.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search