node.js – When using npm, commands fail due to cache access restriction/lock (errno EBUSY -4082)

Problem

I’ve been running into what appears to be a directory access issue in Windows when attempting to use npm. Having tried numerous suggestions from various other SO questions and answers, I thought I’d see if anyone might have suggestions or solutions based on log the output.

I’m continuously running into the error anytime I attempt an npm operation, including install (global or local), uninstall, updateor executing tools such as electron-packager as shown in the package.json scripts section, below the error itself.

Information

  • Operating system: Windows 10 (Build 19043)
  • node version: v18.3.0
  • npm version: 8.12.1

The error output itself is typically :

npm ERR! code EBUSY
npm ERR! syscall rename
npm ERR! path C:Users<username>AppDataLocalnpm-cache_cacachetmpa870f111
npm ERR! dest C:Users<username>AppDataLocalnpm-cache_cacachecontent-v2sha512d323a585f82ed54ccfe948a39da16e212d9b112d87f43f74100e4e56c7f688e670ed506b8e7b57284c0a7c268503abcc5569023fcaa73f00b1c4f7670d5c2537
npm ERR! errno -4082
npm ERR! EBUSY: resource busy or locked, rename 'C:Users<username>AppDataLocalnpm-cache_cacachetmpa870f111' -> 'C:Users<username>AppDataLocalnpm-cache_cacachecontent-v2sha512d323a585f82ed54ccfe948a39da16e212d9b112d87f43f74100e4e56c7f688e670ed506b8e7b57284c0a7c268503abcc5569023fcaa73f00b1c4f7670d5c2537'

npm ERR! A complete log of this run can be found in:
npm ERR!     C:Users<username>AppDataLocalnpm-cache_logs2022-06-04T18_34_40_792Z-debug-0.log

Full log output: PasteBin

package.json


{
    "name": "MyElectronApp",
    "productName": "MyElectronApp",
    "desktopName": "MyElectronApp",
    "version": "1.0.0",
    "description": "Foobar",
    "main": "app.js",
    "repository": "https://github.com/",
    "keywords": [],
    "author": "cognophile",
    "license": "GNU General Public License v3.0",
    "files": [
        "app.js",
        "assets/styles.css",
        "resources/icon.png",
        "resources/icon.ico",
        "resources/icon.icns"
    ],
    "scripts": {
        "start": "npx electron .",
        "build": "npm run build:macos && npm run build:linux && npm run build:windows",
        "build:macos": "npx electron-packager . --overwrite --asar --out=dist --ignore="^media$" --prune --platform=darwin --arch=x64 --icon=resources/icon.icns --app-bundle-id=com.cognophile.myelectronapp --app-version=$npm_package_version && cd dist/MyElectronApp-darwin-x64 && zip -ryXq9 ../MyElectronApp-macos-${npm_package_version}.zip MyElectronApp.app",
        "build:linux": "npx electron-packager . --overwrite --out=dist --ignore="^media$" --prune --platform=linux --arch=x64 --icon=resources/icon.png --app-bundle-id=com.cognophile.myelectronapp --app-version=$npm_package_version && cd dist/MyElectronApp-linux-x64/ && zip -ryq9 ../MyElectronApp-linux-${npm_package_version}.zip *",
        "build:windows": "npx electron-packager . --overwrite --asar --out=dist --ignore="^media$" --prune --platform=win32 --arch=ia32 --icon=resources/icon.ico --version-string.ProductName=$npm_package_productName --app-version=$npm_package_version && cd dist/MyElectronApp-win32-ia32 && zip -ryq9 ../MyElectronApp-windows-${npm_package_version}.zip *"
    },
    "devDependencies": {
        "electron": "^13.6.6",
        "electron-builder": "^22.4.1",
        "electron-packager": "^14.2.1"
    },
    "dependencies": {
        "about-window": "^1.15.2"
    }
}

Trying to run the scripts results in the same error, with differing details. For example, trying to execute the npm start script. The same is true for npm run build:windowsbut the unfetchable package URL in the error is https://registry.npmjs.org/electron-packager

❯ npm start
npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.

> MyElectronApp@1.0.0 start
> npx electron .

npm WARN config global `--global`, `--local` are deprecated. Use `--location=global` instead.
npm ERR! code EBUSY
npm ERR! syscall rename
npm ERR! path C:Users<username>AppDataLocalnpm-cache_cacachetmp1286790
npm ERR! dest C:Users<username>AppDataLocalnpm-cache_cacachecontent-v2sha5121b71c52f6c1e74e5c3c22e0f872bae840fa8e460c356b5c1f807c437a1f8cad2b7bfeb884bb387cbd8012a08d5deaa4b1395f5d4d1cfbc00cfd1b4f5c5e201e3
npm ERR! errno EBUSY
npm ERR! Invalid response body while trying to fetch https://registry.npmjs.org/electron: EBUSY: resource busy or locked, rename 'C:Users<username>AppDataLocalnpm-cache_cacachetmp1286790' -> 'C:Users<username>AppDataLocalnpm-cache_cacachecontent-v2sha5121b71c52f6c1e74e5c3c22e0f872bae840fa8e460c356b5c1f807c437a1f8cad2b7bfeb884bb387cbd8012a08d5deaa4b1395f5d4d1cfbc00cfd1b4f5c5e201e3'

npm ERR! A complete log of this run can be found in:
npm ERR!     C:Users<username>AppDataLocalnpm-cache_logs2022-06-04T19_08_01_775Z-debug-0.log

Attempted solutions

I’ve tried a number of suggestions from a variety of sources, including (source: npm ERR! code ENOENT npm ERR! syscall rename [StackOverflow]):

  • Clearing the cache with npm cache clean --force then reinstalling (npm install)
  • Verifying the cache with npm cache verify then reinstralling (npm install)
  • Removing node_modules and package-lock.jsonclearing the cache, and reinstalling
  • Removing read-only checkbox from project folder and cache folder (right-click > properties) which seems to immediately get reset

I’ve tried these in dedicated PowerShell terminals (non-admin and admin), and the integrated terminal in Visual Studio Code, all having the same result. I’ve also tried:

  • Repairing the nodejs installation via the installer
  • Uninstalling nodejs and remove node/npm related files and directories, restarting the PC, and reinstalling nodejs and attempting npm install in a fresh PowerShell terminal
  • Rebooting to ensure no other processes are using these directories (such as vscode or another terminal instance)
  • Closing anti-virus program (not attempting a full uninstall) and trying the above solutions

I suspect this relates to permissions on the cache directories, judging by the error message, but since it is npm which creates those directories after the npm cache cleanI’m not sure I understand what more is needed, and am naturally hesitant to change directory permissions (eg NodeJS change file and folder permissions [StackOverflow]) until having received advice.

Thanks in advance!

Leave a Comment