Skip to content
Uses a breadth-first search, when necessary, to resolve the file path using the correct casing, and verify that the file actually exists. Returns consistent results regardless of case sensitivity of the file path and/or operating system.
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Type Name Latest commit message Commit time
Failed to load latest commit information.
appveyor.yml add node versions to CI configs Aug 6, 2017

detect-file NPM version NPM monthly downloads NPM total downloads Linux Build Status Windows Build Status

Detects if a file exists and returns the resolved filepath.


Install with npm:

$ npm install --save detect-file

Install with yarn:

$ yarn add detect-file


var detect = require('detect-file');



Detect the given filepath if it exists.


  • filepath {String}: filepath to detect.
  • options {Object}: Additional options.
  • options.nocase {Boolean}: Set this to true to force case-insensitive filename checks. This is useful on case sensitive file systems.
  • returns {String}: Returns the detected filepath if it exists, otherwise returns null.


var res = detect('package.json');
//=> "package.json"

var res = detect('fake-file.json');
//=> null

Case sensitive file systems

When using the nocase option, this library will attempt to detect the filepath with the following methods:

  1. Try to read all files in the filepath using fs.readdirSync. If successful and filepath is a directory, return the filepath.
  2. Try to read all files in the filepath's directory using fs.readdirSync. If successful, do case insensitive comparasions of the filepath to the files in filepath's directory.


Related projects

fs-exists-sync: Drop-in replacement for fs.existsSync with zero dependencies. Other libs I found either have crucial differences… more | homepage


Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.

Building docs

(This project's is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the readme template.)

To generate the readme, run the following command:

$ npm install -g verbose/verb#dev verb-generate-readme && verb

Running tests

Running and reviewing unit tests is a great way to get familiarized with a library and its API. You can install dependencies and run tests with the following command:

$ npm install && npm test


Brian Woodward


Copyright © 2017, Brian Woodward. Released under the MIT License.

This file was generated by verb-generate-readme, v0.6.0, on August 05, 2017.

You can’t perform that action at this time.