Debugging npm Install Failures: Logs, ENOTEMPTY, and Safe Recovery

February 19, 2025

An npm installation problem during development led me to collect notes about logs and the ENOTEMPTY error. The notes included a Stack Overflow example and a script that repeatedly deleted directories mentioned in the log before retrying installation.

Revisiting that script made the problem more interesting. Its recovery strategy was understandable, but its path handling could turn a package cleanup into deletion of an unintended directory. This article follows the investigation from the original error to a more deliberate recovery process.

Historical note: the front matter preserves the original npm.md file's creation and modification times. Source verification and script analysis were added during later consolidation. The copied error belongs to another developer; my notes do not preserve my own npm version, complete log, or final repair command.

Start with the installation log

The first saved link asks how to inspect an npm installation that appears stuck. It is general diagnostic guidance, rather than a question specifically about ENOTEMPTY.1

For another installation attempt, detailed output can help:

npm install --loglevel verbose

The older answer's description of a local npm-debug.log needs qualification. npm 10 documents the default location as:

a directory named _logs inside the npm cache.

— npm, “Setting Log File Location.”2

The logs-dir setting can override that location. Use the path printed for the failing command rather than assuming the last file in ~/.npm/_logs belongs to this project. Record node --version, npm --version, the working directory, and the command that failed alongside the log.

Two additional options answer different questions:

# Show output from installation lifecycle scripts.
npm install --foreground-scripts

# Include timing information and print the debug-log path.
npm install --timing

These commands perform an installation; they are diagnostic options for a deliberate retry, not read-only log viewers. npm began hiding lifecycle-script output by default in version 7.2

Read the operation as well as the error code

The saved question was posted by Memphis Meng in June 2021. Its relevant fields were code ENOTEMPTY, syscall rename, a package directory, and a hidden destination directory.3 With the project path shortened, the operation looked like this:

node_modules/@babel/plugin-proposal-decorators
    → node_modules/@babel/.plugin-proposal-decorators-ysLLPQFw

ENOTEMPTY indicates that an operation requiring an empty directory encountered a nonempty one.4 In this example, the operation was a rename. This points to installation-directory state; the error alone does not establish a network failure, a dependency-version conflict, or a corrupt download cache.

The hidden destination has a purpose. npm's Arborist implementation moves packages that need replacement out of the way so it can roll back on failure; its retirement-path helper constructs names of the form .name-hash.5 A previous interrupted installation leaving conflicting state is a plausible explanation. Without the original environment and full log, it remains an explanation to investigate rather than a proven cause of my incident.

Version matters here. The implementation inspected during consolidation already handles some EEXIST and ENOTEMPTY cases by removing the destination and retrying the move. That does not prove every such failure is resolved, but it is another reason to avoid treating a historical workaround as mandatory for every npm version.5

How the saved recovery script worked

Eran Or's answer suggested deleting the specific hidden destination named in the error and retrying installation.6 A later answer automated repeated cleanup. Its author explained the motivation:

I had the same problem while having a slow and unreliable Internet connection.

— User (2022), opening sentence.7

The script selected a log, extracted paths from ENOTEMPTY lines, deleted them with rm -rf, and ran npm install again until it succeeded. It was intended to preserve more of the existing installation than deleting all of node_modules.

That intent is useful context. The implementation needs closer inspection before reuse.

Quoting the deletion argument did not protect the original path

The original extraction loop used this header:

# Historical excerpt for analysis; this is not a cleanup command to run.
for path in `cat "$log" | grep 'ENOTEMPTY' | grep -oE "[^']+node_modules[^']+"`; do

Because the command substitution is unquoted, Bash splits its output on whitespace before assigning each value to path. The quoted "$path" in the subsequent deletion preserves each already-split fragment; it cannot reassemble the original pathname.

The example from the question contains a directory named React Programming. Testing only the extraction, with deletion replaced by printed output, produced fragments such as:

/Users/memphismeng/Documents/React
Programming/Foot-in-The-Door/mobile/fitd/node_modules/@babel/plugin-proposal-decorators

The first fragment is outside the intended package directory. The second is now a relative path. A script that deletes those fragments can affect something quite different from the package named in the error.

The script had broader scope than its stated repair

The regular expression also matched both the source and destination paths in the rename message. It did not restrict deletion to the hidden destination suggested by the earlier answer.

Other assumptions were unresolved:

  • The selected log might belong to another project or npm command.
  • The cache and log locations were hardcoded.
  • There was no check that an extracted path remained inside the intended project's dependency directory.
  • The retry loop had no limit, backoff, or test that the next failure was still ENOTEMPTY.

The script began with set -e, but npm install was the condition of an if statement. A failure there takes the unsuccessful branch and allows the loop to continue. It does not automatically terminate the script. A bounded local example using false in the same position confirms that behavior without running npm.

Any future automation should operate on an explicitly selected log, preserve complete paths, validate the cleanup boundary, and stop when the failure changes. For a one-off repair, inspecting the actual destination is simpler than trusting this extraction loop.

Keep installation files, the lockfile, and the cache separate

These are different kinds of state:

ItemRoleConsequence for recovery
node_modulesInstalled dependency filesContains the directories involved in this rename failure
Project package-lock.jsonRecords the resolved dependency treePreserve it when the goal is to restore the same dependencies
npm cacheStores downloaded data and package-related contentCan help a reinstall; clearing it is a separate operation

The project lockfile is intended to be committed and used to reproduce the dependency tree.8 Deleting it while repairing installation files can introduce new dependency resolutions and make it harder to distinguish recovery from an upgrade.

For an existing project whose lockfile matches package.json, a clean installation is an alternative to repeated partial cleanup:

npm ci

This removes the existing node_modules before installing. It requires a lockfile, fails on a manifest/lockfile mismatch, and does not rewrite either file. Flags that shaped the lockfile, such as --legacy-peer-deps, may also need to match the project's established configuration.9

What if you want to add one package and keep the versions already installed? Dan asked this after seeing other packages change when adding a dependency.10 Adding a package with npm install <name> changes the dependency tree. Review the diff in package.json and package-lock.json to see which entries changed. Use npm ci when you want to reinstall the tree already recorded in the lockfile.9

If preserving the partial installation matters, first stop competing installation commands and inspect the exact path and dest from the failing run. Confirm that the destination is the stale retirement directory in this project before removing it and retrying. A broad deletion pattern is not a substitute for identifying the conflicting directory.

The original script's discussion of download savings should remain a motivation rather than an asymptotic performance claim. Rebuilding node_modules does not necessarily require downloading every package again: npm has a separate cache. Actual work depends on cache availability, package sizes, extraction, and installation scripts. npm documents cache integrity checking and describes routine cache clearing as generally unnecessary.11

Avoid unrelated repair commands

Some answers on the same question suggest npm remove node_modules or npm remove package-lock.json. These are not general filesystem-deletion commands: remove is an alias for npm uninstall, which operates on package specifications and can update dependency manifests.12

Likewise, the original question's unsuccessful npm audit fix --legacy-peer-deps attempt does not establish a repair for directory state. npm audit fix addresses reported dependency vulnerabilities, while legacy-peer-deps changes how peer dependencies are handled. Neither option means “clear the conflicting rename destination.”1314

The useful sequence is to capture the failure, identify the operation and paths, preserve dependency intent, and choose either a narrowly scoped cleanup or a clean installation. If the error persists, investigate the recorded npm version and filesystem behavior instead of repeating increasingly broad deletion commands.

For a different class of failure after dependencies are installed, see the note on fully specified module imports.

References

Footnotes

  1. papakias. (2016, September 9). How to see logs from npm installation? [Question and answers]. Stack Overflow. Includes Atul Sharma's answer from the same date and Zach Lysobey's answer from September 15, 2024. https://stackoverflow.com/questions/39412424/how-to-see-logs-from-npm-installation ↩

  2. npm. (n.d.). Logging (CLI v10 documentation). “Setting Log File Location,” second paragraph; “foreground-scripts”; “Timing Information.” https://docs.npmjs.com/cli/v10/using-npm/logging/ ↩ ↩2

  3. Meng, M. (2021, June 15). npm install error ENOTEMPTY: directory not empty, [Question]. Stack Overflow. Error output in the question body. https://stackoverflow.com/questions/67991537/npm-install-error-enotempty-directory-not-empty ↩

  4. Node.js contributors. (n.d.). Errors. Node.js documentation. “Common system errors,” ENOTEMPTY. https://nodejs.org/api/errors.html#common-system-errors ↩

  5. npm contributors. (n.d.). Arborist installation and retirement-path implementation [Source code]. GitHub, npm/cli, latest branch inspected during consolidation. _retireShallowNodes, _renamePath, and retirePath. https://github.com/npm/cli/blob/latest/workspaces/arborist/lib/arborist/reify.js ; https://github.com/npm/cli/blob/latest/workspaces/arborist/lib/retire-path.js ↩ ↩2

  6. Or, E. (2021, October 21). Answer to “npm install error ENOTEMPTY: directory not empty,” [Answer]. Stack Overflow. Targeted removal of the hidden destination directory. https://stackoverflow.com/a/69668434 ↩

  7. User. (2022, November 12). Answer to “npm install error ENOTEMPTY: directory not empty,” [Answer; edited November 1, 2023]. Stack Overflow. Opening paragraph and Bash cleanup loop. https://stackoverflow.com/a/74413191 ↩

  8. npm. (n.d.). package-lock.json (CLI v10 documentation). “Description.” https://docs.npmjs.com/cli/v10/configuring-npm/package-lock-json/ ↩

  9. npm. (n.d.). npm ci (CLI v10 documentation). “Description,” differences from npm install and note on installation flags. https://docs.npmjs.com/cli/v10/commands/npm-ci/ ↩ ↩2

  10. Dan. (2018, February 21). NPM - How to install a new package without update or add packages described on package.json [Question]. Stack Overflow. Question and example. ↩

  11. npm. (n.d.). npm cache (CLI v10 documentation). “Description,” “Details,” and “A note about the cache's design.” https://docs.npmjs.com/cli/v10/commands/npm-cache/ ↩

  12. npm. (n.d.). npm uninstall (CLI v10 documentation). “Synopsis” and “Description.” https://docs.npmjs.com/cli/v10/commands/npm-uninstall/ ↩

  13. npm. (n.d.). npm audit (CLI v10 documentation). “Description.” https://docs.npmjs.com/cli/v10/commands/npm-audit/ ↩

  14. npm. (n.d.). Config (CLI v10 documentation). “legacy-peer-deps.” https://docs.npmjs.com/cli/v10/using-npm/config/#legacy-peer-deps ↩

© 2025 borui. All rights reserved. This content may be freely reproduced, displayed, modified, or distributed with proper attribution to borui and a link to the article: borui (2025-02-19). Debugging npm Install Failures: Logs, ENOTEMPTY, and Safe Recovery. https://caiborui.com/blog/2025-02-19-en-debugging-npm-install-enotempty

@misc{borui2025-e9ad026c,
  author = {borui},
  title = {{Debugging npm Install Failures: Logs, ENOTEMPTY, and Safe Recovery}},
  year = {2025},
  publisher = {borui's blog},
  url = {https://caiborui.com/blog/2025-02-19-en-debugging-npm-install-enotempty}
}