npm install leaves file handles on the folder after end of process (Windows)

What I Wanted to Do

From a node.js program I wanted to do the following steps in sequence:

  1. run npm install inside of a folder A
  2. rename folder A to B

What Happened Instead

The system (Windows 10) reported EBUSY, because there were still open node.js processes with handles onto that folder, even though the child-process executing npm install has ended correctly.

Reproduction Steps

An Example to reproduce this on a Windows 10 System:

  1. git clone https://github.com/npm/cli.git
  2. cd cli
  3. npm install
  4. (wait until process is finished in cmd)
  5. cd … (so that not the cmd has a handle on the folder)
  6. rmdir /s /q cli

You will see that the folder cannot be renamed, because some processes are still using it. With the Process Explorer you can find out, that those are node.js processes.
Now you can do the following:

  1. taskkill /f /im node.exe
  2. rmdir /s /q cli

The folder was removed now.

Details

I would expect npm install to not exit until all (sub)processes are completely finished and all handlers are relieved.

Potential conflicting virus scanners on my system were deactivated.

Platform Info

$ npm --versions
{ npm: '6.9.0',             
  ares: '1.15.0',           
  brotli: '1.0.7',          
  cldr: '35.1',             
  http_parser: '2.8.0',     
  icu: '64.2',              
  modules: '64',            
  napi: '4',                
  nghttp2: '1.34.0',        
  node: '10.16.0',          
  openssl: '1.1.1b',        
  tz: '2019a',              
  unicode: '12.1',          
  uv: '1.28.0',             
  v8: '6.8.275.32-node.52', 
  zlib: '1.2.11' }    
$ node -p process.platform
win32