[BUG] npm install will randomly hang forever and cannot be closed when this occurs
Problem
Is there an existing issue for this? - [X] I have searched the existing issues This issue exists in the latest npm version - [X] I am using the latest npm Current Behavior When running `npm install` it will sometimes hang at a random point. When it does this, it is stuck forever. CTRL+C will do nothing the first time that combination is pressed when this has occurred. Pressing that key combination the second time will make the current line (the one showing the little progress bar) disappear but that's it. No further responses to that key combination are observed. The CMD (or Powershell) window cannot be closed regardless. The process cannot be killed by Task Manager either (Access Denied, although I'm an Administrator user so I'd assume the real reason is something non-permissions related). The only way I have found to close it is to reboot the machine. My suspicion is it's some sort of deadlock, but this is a guess and I have no idea how to further investigate this. I've tried using Process Explorer to check for handles to files in the project directory from other processes but there are none. There are handles held by the Node process npm is using, and one for the CMD window hosting it, but that's it. Even running with `log-level silly` yields no useful information. When it freezes there are no warnings or errors, it just sits on the line it was on. This is some log output from one of the times when it got stuck (I should again emphasise that the point whe
Unverified for your environment
Select your OS to check compatibility.
2 Fixes
Implement npm install Timeout and Process Management
The hanging issue during 'npm install' is likely caused by a deadlock situation where npm is waiting for a resource that is not being released due to an internal process conflict or a network issue. This can occur when npm tries to access a package that is locked or when there is a problem with the registry connection. Additionally, the inability to terminate the process may be due to how Windows handles certain subprocesses, especially when they are in a waiting state.
Awaiting Verification
Be the first to verify this fix
- 1
Set a Timeout for npm Install
Use the npm command with a timeout option to prevent it from hanging indefinitely. This can be done by wrapping the npm install command in a timeout command.
bashtimeout /t 60 && npm install - 2
Check for Locked Files
Use a tool like 'Handle' from Sysinternals to check for any locked files that might be causing npm to hang. This can help identify if another process is interfering with npm.
bashhandle <project-directory> - 3
Clear npm Cache
Sometimes the npm cache can become corrupted. Clear the cache to ensure that npm is not using any stale or corrupted files.
bashnpm cache clean --force - 4
Use Verbose Logging
Run npm install with verbose logging to capture more detailed output that may help identify where the process is hanging.
bashnpm install --verbose - 5
Upgrade Node.js and npm
Ensure that both Node.js and npm are updated to their latest stable versions, as this may resolve known bugs that could be causing the hang.
bashnpm install -g npm@latest && nvm install latest
Validation
To confirm the fix worked, run 'npm install' on your project after implementing the above steps. Ensure that it completes successfully without hanging. Monitor the process for any signs of hanging and check the logs for any errors or warnings.
Sign in to verify this fix
1 low-confidence fix
Implement npm install Timeout and Process Management
The hanging issue during 'npm install' is likely caused by a deadlock situation or a blocking operation in the npm process, which can occur due to various reasons such as network issues, corrupted package cache, or conflicts with other processes. The inability to terminate the process suggests that it may be stuck in a state where it cannot respond to interrupts, possibly due to unhandled promises or synchronous operations that block the event loop.
Awaiting Verification
Be the first to verify this fix
- 1
Set npm Timeout Configuration
Configure npm to have a timeout for install operations to prevent indefinite hanging. This can be set in the npm configuration file or via command line.
bashnpm config set timeout 60000 - 2
Clear npm Cache
Clear the npm cache to remove any potentially corrupted files that may cause the install process to hang.
bashnpm cache clean --force - 3
Use Verbose Logging
Run npm install with verbose logging to gather more information about what happens before it hangs. This can help identify the specific package or operation causing the issue.
bashnpm install --verbose - 4
Check for Conflicting Processes
Use tools like Process Explorer to identify if other processes are interfering with npm. Look for any node processes that might be holding resources.
bashtasklist | findstr node - 5
Upgrade Node.js and npm
Ensure that both Node.js and npm are updated to the latest stable versions, as this may resolve underlying issues that cause hangs.
bashnpm install -g npm@latest && nvm install latest
Validation
To confirm the fix worked, run 'npm install' on your project and observe if it completes successfully within the set timeout. Additionally, monitor for any hangs or issues during the process. If the timeout triggers, it should provide a message indicating the timeout occurred.
Sign in to verify this fix
Environment
Submitted by
Alex Chen
2450 rep