Skip to content

Cross-platform utility to open a file or directory in the system's default file manager (Finder, Explorer, Nautilus, etc.)

Notifications You must be signed in to change notification settings

jonschlinkert/open-file-manager

Repository files navigation

open-file-manager NPM version NPM monthly downloads NPM total downloads

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

Install with npm:

$ npm install --save open-file-manager

Usage

import { openFileManager } from "open-file-manager";
// or
import openFileManager from "open-file-manager";

API

openFileManager

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);
}

CLI

# 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

Examples

Open a project's dist folder

import { openFileManager } from "open-file-manager";

await openFileManager("./dist");

Open a file's location

// This will open the file manager and select/highlight the file
await openFileManager("./README.md");

Use in npm scripts

{
  "scripts": {
    "open:dist": "open-file-manager ./dist",
    "open:current": "open-file-manager"
  }
}

Cross-platform usage

import { openFileManager } from "open-file-manager";
import path from "node:path";

// Works on Windows, macOS, and Linux
await openFileManager('/path/to/dir');

Notes

  • 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

Related

You might also be interested in:

About

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

Author

Jon Schlinkert

License

Copyright © 2025, Jon Schlinkert. Released under the MIT License.


This file was generated by verb-generate-readme, v0.8.0, on May 25, 2025.

About

Cross-platform utility to open a file or directory in the system's default file manager (Finder, Explorer, Nautilus, etc.)

Topics

Resources

Stars

Watchers

Forks

Sponsor this project

 

Packages

No packages published