Skip to content

fix(common): regression in loading file-type under Windows OS#16187

Merged
kamilmysliwiec merged 4 commits into
nestjs:masterfrom
iamkanguk97:fix/file-type-under-window-os
Jan 15, 2026
Merged

fix(common): regression in loading file-type under Windows OS#16187
kamilmysliwiec merged 4 commits into
nestjs:masterfrom
iamkanguk97:fix/file-type-under-window-os

Conversation

@iamkanguk97

@iamkanguk97 iamkanguk97 commented Jan 12, 2026

Copy link
Copy Markdown
Contributor

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Other... Please describe:

What is the current behavior?

Issue Number: #16179

Error Message

On Windows OS, FileTypeValidator fails when dynamically loading the file-type ESM package with the following error:

Error [ERR_UNSUPPORTED_ESM_URL_SCHEME]: Only URLs with a scheme in: file, data, and node are supported by the default ESM loader. On Windows, absolute paths must be valid file:// URLs. Received protocol 'c:'

Causes

  • Window OS Absolute Paths: (e.g., D:\Users\Project\node_modules\file-type\index.js)
  • This absolute paths are passed directly to the ESM dynamic import. The ESM loader(in project, load-esm package) interprets the drive letter(e.g., D:) as an invalid URL Scheme instead of recognizing it as a file path.
  • This regression was introduced in PR fix(common): resolve file-type path explicitly #16077 when file-type package migrated to ESM-only.

What is the new behavior?

Windows absolute paths are now converted to proper file:// URLs using Node.js's built-in pathToFileURL() function before being passed to loadEsm().

// Before
let fileTypePath: string;
try {
  fileTypePath = require.resolve('file-type');
} catch {
  fileTypePath = 'file-type';
}

// After
import { pathToFileURL } from 'url';

let fileTypeModule: string;
try {
  const resolvedPath = require.resolve('file-type');
  fileTypeModule = pathToFileURL(resolvedPath).href;  // Convert to file:// URL
} catch {
  fileTypeModule = 'file-type';
}

This ensures cross-platform ESM compatibility:

  • Windows: D:\path\to\file-typefile:///D:/path/to/file-type
  • Unix/macOS: /path/to/file-typefile:///path/to/file-type

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Testing

  • All existing FileTypeValidator tests pass on Ubuntu, macOS, and Windows
  • Verified on Windows environment using GitHub Actions (I'm on macOS, so tested via CI)
  • No additional dependencies required - uses Node.js built-in url.pathToFileURL()

Technical Details

  • ESM loader officially supports file:// URLs on all platforms
  • pathToFileURL() is available in all supported Node.js versions
  • This approach follows Node.js ESM documentation recommendations for handling platform-specific absolute paths

Note

  • This is my first open source contribution to NestJS, and I'm still learning the contribution process.
  • Please let me know if any changes are needed!

This adds GitHub Actions workflow to test on Ubuntu, Windows, and macOS.
This is optional and can be removed if the maintainers prefer.

Related to nestjs#16179
Uses pathToFileURL to convert file system paths to proper URLs
before passing to the ESM loader, ensuring cross-platform compatibility.

Fixes nestjs#16179
@coveralls

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 03b1f073-5d58-402b-8343-1f264a8368ea

Details

  • 4 of 5 (80.0%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.01%) to 89.749%

Changes Missing Coverage Covered Lines Changed/Added Lines %
packages/common/pipes/file/file-type.validator.ts 4 5 80.0%
Totals Coverage Status
Change from base Build 8ad6ab71-590f-4cc8-9404-19a9affa9c9a: -0.01%
Covered Lines: 7442
Relevant Lines: 8292

💛 - Coveralls

@kamilmysliwiec kamilmysliwiec merged commit 09e96bd into nestjs:master Jan 15, 2026
2 checks passed
@kamilmysliwiec

Copy link
Copy Markdown
Member

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants