npm-root
Show npm package installation root directory
TLDR
Display path to the local node_modules directory
Display path to the global node_modules directory
SYNOPSIS
npm root [--global|-g] [--prefix path] [--json] [--parseable]
PARAMETERS
--global, -g
Shows the global node_modules directory path, where packages installed with npm install -g would reside.
--prefix path
Displays the node_modules directory path associated with the specified prefix path, overriding the default project or global prefix.
--json
Outputs the path as a JSON string. Useful for programmatic consumption.
--parseable
Outputs the path in a machine-parseable format (a single line). Useful for scripting.
DESCRIPTION
npm root is an npm subcommand that prints the absolute path to the node_modules directory. This directory is where npm installs project dependencies locally or global packages if specified.
By default, when run within an npm project, it shows the local node_modules path relative to the current project's root. When the --global or -g option is used, it outputs the path to the global node_modules directory, where globally installed packages reside.
This command is highly useful for scripting, debugging package resolution issues, or simply understanding where npm manages your dependencies. It reflects npm's current configuration, including any prefix settings, and does not perform any installation or modification to the filesystem.
CAVEATS
The output of npm root is dynamic; it depends on the current working directory for local roots and npm configuration (like prefix) for global roots.
It does not create the directory if it doesn't already exist. Running it in a directory not part of an npm project might output a path higher up the directory tree or the global root based on npm's fallback logic.
LOCAL VS. GLOBAL ROOT
When npm root is run without --global, it determines the local node_modules path, which is typically <current_project_root>/node_modules. This is where dependencies listed in package.json are installed.
With --global, it reports the system-wide global node_modules path, usually configured by npm during installation or via the prefix configuration. This is where executables for global commands are often linked from.
CONFIGURATION INFLUENCE
The exact path returned by npm root --global can be influenced by the prefix configuration. You can inspect this value using npm config get prefix. Setting a custom prefix will change where npm installs global packages and, consequently, what npm root --global reports.
HISTORY
npm root has been a fundamental command within the npm ecosystem since its early versions, serving the essential purpose of reporting package installation locations. Its core functionality has remained consistent, with additions primarily focusing on output formatting options like --json and --parseable to enhance its utility in scripting and automation.
SEE ALSO
npm(1), npm-bin(1), npm-prefix(1), npm-config(1), npm-install(1)