By default, npm install <pkg> (local mode) installs packages into ./node_modules inside your current project directory (and records dependencies in package.json / package-lock.json), while npm install -g <pkg> (global mode) installs packages into npm’s global prefix directory—such as /usr/local/lib/node_modules (or your NVM/Homebrew prefix) on macOS/Linux and %AppData%\npm\node_modules on Windows—with executable CLI shims placed alongside node_modules or in the global bin folder (npm root -g and npm prefix -g show the exact active paths on your machine).


1. Local Package Locations (./node_modules)

When you run npm install without -g, npm looks for the nearest project root containing package.json (or uses the current working directory if none exists yet) and writes files to node_modules:

// Inspect local project root and expected node_modules path in Node.js
const path = require("node:path");

function getLocalNodeModulesPath(projectDir = process.cwd()) {
  if (typeof projectDir !== "string" || projectDir.trim().length === 0) {
    throw new TypeError("Expected projectDir to be a non-empty string.");
  }

  const resolvedRoot = path.resolve(projectDir);
  return {
    projectRoot: resolvedRoot,
    nodeModulesDir: path.join(resolvedRoot, "node_modules"),
    packageJsonPath: path.join(resolvedRoot, "package.json")
  };
}

// Example usage:
console.log(getLocalNodeModulesPath());
  • Project-scoped isolation: Each project gets its own ./node_modules directory, preventing version conflicts across apps.
  • Local CLI binaries: Executables exposed by installed packages live in ./node_modules/.bin (and run automatically via npx <cmd> or npm scripts in package.json).

2. Global Package Locations (npm root -g and npm prefix -g)

When you run npm install -g <pkg>, npm installs packages into the global node_modules directory determined by npm config get prefix (or npm prefix -g):

// Query npm's active global prefix and global node_modules paths safely
const { execFileSync } = require("node:child_process");
const path = require("node:path");

function getGlobalNpmPaths() {
  const prefix = execFileSync("npm", ["prefix", "-g"], { encoding: "utf8" }).trim();
  const root = execFileSync("npm", ["root", "-g"], { encoding: "utf8" }).trim();

  if (!prefix || !root) {
    throw new Error("Unable to resolve npm global paths.");
  }

  const binDir = process.platform === "win32" ? prefix : path.join(prefix, "bin");
  return { prefix, root, binDir };
}

// Example usage:
// console.log(getGlobalNpmPaths());
  • Common platform defaults:
    • Linux / macOS (system Node): Global node_modules is often /usr/local/lib/node_modules (or /opt/homebrew/lib/node_modules on Apple Silicon Homebrew) with CLI links in <prefix>/bin.
    • Windows: Global node_modules is typically %AppData%\npm\node_modules (C:\Users\<User>\AppData\Roaming\npm\node_modules), with .cmd / PowerShell shims in %AppData%\npm.
  • Version managers (nvm, fnm, volta, asdf): Node version managers isolate global packages per Node version or toolchain directory so switching Node versions does not silently mix global binaries.

3. Verifying and Customizing Paths Without Mutating System State

Instead of guessing paths across OSes or version managers, query npm root -g / npm prefix -g or inspect process.env and process.execPath read-only:

// Read-only summary of current Node runtime and environment paths
function getRuntimePathSnapshot(env = process.env) {
  if (!env || typeof env !== "object") {
    throw new TypeError("Expected env to be an object.");
  }

  return {
    cwd: process.cwd(),
    execPath: process.execPath,
    platform: process.platform,
    npmConfigPrefix: env.npm_config_prefix || null
  };
}

// Example usage:
console.log(getRuntimePathSnapshot());
// Run in terminal to check exact npm directories:
// npm prefix -g
// npm root -g
  • Check npm root -g first: Because custom prefixes, Homebrew, and version managers change defaults, npm root -g always reports where -g packages are actually placed.
  • Prefer local installs or npx: Installing tools locally (npm install -D <tool>) or running via npx avoids global permission issues (EACCES) and keeps projects reproducible.

TL;DR: Local npm install installs into ./node_modules (with binaries in ./node_modules/.bin), while npm install -g installs into npm root -g (such as /usr/local/lib/node_modules, /opt/homebrew/lib/node_modules, or %AppData%\npm\node_modules, depending on OS and version manager).