Cross-platform utility to open a file or directory in the system's default file manager (Finder, Explorer, Nautilus, etc.)
Please consider following this project's author, Jon Schlinkert, and consider starring the project to show your ❤️ and support.
Install with npm:
$ npm install --save open-file-manager
import { openFileManager } from "open-file-manager";
// or
import openFileManager from "open-file-manager";
Opens a file or directory in the system's default file manager.
export const openFileManager = async (dirname?: string): Promise<void>;
Basic usage:
// Open current directory
await openFileManager();
// Open specific directory
await openFileManager("/home/user/documents");
// Open file location (selects the file in the file manager)
await openFileManager("./package.json");
Parameters:
pathname (optional)
- Type:
string
- Default:
'.'
(current directory) - Path to the file or directory to open. Can be absolute or relative.
Returns:
- Type:
Promise<void>
- Resolves when the file manager opens successfully
- Throws an error if the operation fails
Platform behavior:
- Windows: Uses
explorer.exe
. When opening a file, it selects the file in Explorer. - macOS: Uses
open -R
to reveal the item in Finder. - Linux/Unix: Tries multiple file managers in order: xdg-open, nautilus, dolphin, thunar, pcmanfm, nemo.
Error handling:
try {
await openFileManager("/path/to/file");
} catch (error) {
console.error("Failed to open file manager:", error.message);
}
# Open current directory
open-file-manager
# Open specific directory
open-file-manager /home/user/documents
# Open file location
open-file-manager ./package.json
# Show help
open-file-manager --help
# Show version
open-file-manager --version
import { openFileManager } from "open-file-manager";
await openFileManager("./dist");
// This will open the file manager and select/highlight the file
await openFileManager("./README.md");
{
"scripts": {
"open:dist": "open-file-manager ./dist",
"open:current": "open-file-manager"
}
}
import { openFileManager } from "open-file-manager";
import path from "node:path";
// Works on Windows, macOS, and Linux
await openFileManager('/path/to/dir');
- All paths are resolved relative to the current working directory
- The function is async and returns a Promise
- On Windows, if a file doesn't exist, it will open the parent directory instead
- On Linux, the function will try multiple file managers and use the first one available
- Supports FreeBSD, OpenBSD, and SunOS in addition to major platforms
You might also be interested in:
- open-finder-dialog: Open a finder dialog window (finder prompt) programmatically. Only works on MacOS. | homepage
- open-linux-file-dialog: Open a file dialog window programmatically to allow the user to select one or more… more | homepage
- open-windows-file-dialog: Programmatically open a file dialog window (explorer) for picking files. Only works on Windows. Also… more | homepage
Contributing
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue.
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
Building docs
(This project's readme.md is generated by verb, please don't edit the readme directly. Any changes to the readme must be made in the .verb.md readme template.)
To generate the readme, run the following command:
$ npm install -g verbose/verb#dev verb-generate-readme && verb
Jon Schlinkert
Copyright © 2025, Jon Schlinkert. Released under the MIT License.
This file was generated by verb-generate-readme, v0.8.0, on May 25, 2025.