LinuxCommandLibrary

npm-explore

Explore installed package directory

TLDR

Browse an installed package

$ npm explore [package_name]
copy

Browse a package and run a command inside it
$ npm explore [package_name] -- [command]
copy

SYNOPSIS

npm explore <package-name>[@<version>] [-- <command>]

PARAMETERS

<package-name>
    The name of the installed package to explore. This package must be present in the local `node_modules`.

@<version>
    An optional version specifier for the package. If omitted, the locally installed version will be used.

-- <command>
    The command to execute within the package's directory. If this option is not provided, an interactive shell will be opened instead.

DESCRIPTION

The npm explore command provides a convenient way to navigate into an installed Node.js package's directory, either to inspect its contents or execute commands within its context. When invoked, it temporarily changes the current working directory to the root of the specified package. This is particularly useful for debugging, examining the source code, or making temporary modifications to a package without globally modifying your project's `node_modules` structure.

If a command is provided after --, npm explore executes that command within the package's directory and then reverts to the original working directory. If no command is specified, it opens an interactive shell (e.g., `bash`, `sh`, or `cmd`) inside the package's directory, allowing for manual exploration and execution of multiple commands. This temporary environment is invaluable for quick tests or deep dives into package internals.

CAVEATS

The changes made during an npm explore session are temporary and local to the package's directory. They are not persisted if you were to reinstall or update the package. This command requires the specified package to be already installed in your `node_modules` (or globally, if navigating manually). There is no explicit `--global` flag for npm explore itself; to explore a globally installed package, you would typically `cd` into the global `node_modules` directory first, often located using `npm root -g`.

INTERACTIVE MODE

If no command is specified after --, npm explore will open an interactive shell (e.g., `bash`, `sh`, or `cmd.exe` depending on your system) directly within the specified package's directory. This allows you to browse files, run tests, or execute arbitrary commands within that package's context. To exit this interactive session and return to your original working directory, simply type `exit` in the shell.

EXAMPLES

1. Open an interactive shell in a package:

npm explore lodash

2. Run a command inside a package's directory:

npm explore express -- npm test

3. List files in a package:

npm explore react -- ls -la

HISTORY

The npm explore command has been a part of npm's utility set for a considerable time, serving as one of the earlier mechanisms for direct interaction with installed package files. While newer commands like npm exec offer more streamlined ways to run package binaries, npm explore retains its unique value for interactive inspection and debugging directly within a package's source tree, particularly when an interactive shell is desired. Its usage has shifted from a primary tool for executing package scripts to a niche but powerful debugging and exploration utility.

SEE ALSO

npm exec(1), npm root(1), cd(1), sh(1), bash(1)

Copied to clipboard