"npm install -g npm@latest" corrupted my npm installation - Windows 8.1

Viewed 770

Last month I installed node 10.16.3 LTS and because it didn't have latest npm version I've updated it using this official guide: https://docs.npmjs.com/try-the-latest-stable-version-of-npm (option 2):

  1. removed both %ProgramFiles%\nodejs\npm and %ProgramFiles%\nodejs\npm.cmd
  2. moved 'npmrc' file from %ProgramFiles%\nodejs\node_modules\npm to %appdata%\npm\node_modules\npm


Today, after I've installed 'create-react-app', npm showed me message that new minor version of npm is available 6.11.3 -> 6.12.0 and that I can update it.
I ran 'npm install -g npm@latest' but that just threw me bunch of errors:

C:\Users\Wladyslaw\AppData\Roaming\npm\npx -> C:\Users\Wladyslaw\AppData\Roaming\npm\node_modules\npm\bin\npx-cli.js
npm ERR! code EPERM
npm ERR! syscall open
npm ERR! path C:\Users\Wladyslaw\AppData\Roaming\npm\npm
npm ERR! errno -4048
npm ERR! Error: EPERM: operation not permitted, open 'C:\Users\Wladyslaw\AppData\Roaming\npm\npm'
npm ERR!  { [Error: EPERM: operation not permitted, open 'C:\Users\Wladyslaw\AppData\Roaming\npm\npm']
npm ERR!   cause:
npm ERR!    { Error: EPERM: operation not permitted, open 'C:\Users\Wladyslaw\AppData\Roaming\npm\npm'
npm ERR!      errno: -4048,
npm ERR!      code: 'EPERM',
npm ERR!      syscall: 'open',
npm ERR!      path: 'C:\\Users\\Wladyslaw\\AppData\\Roaming\\npm\\npm' },
npm ERR!   stack:
npm ERR!    'Error: EPERM: operation not permitted, open \'C:\\Users\\Wladyslaw\\AppData\\Roaming\\npm\\npm\'',
npm ERR!   errno: -4048,
npm ERR!   code: 'EPERM',
npm ERR!   syscall: 'open',
npm ERR!   path: 'C:\\Users\\Wladyslaw\\AppData\\Roaming\\npm\\npm' }
npm ERR!
npm ERR! The operation was rejected by your operating system.
npm ERR! It's possible that the file was already in use (by a text editor or antivirus),
npm ERR! or that you lack permissions to access it.
npm ERR!
npm ERR! If you believe this might be a permissions issue, please double-check the
npm ERR! permissions of the file and its containing directories, or try running
npm ERR! the command again as root/Administrator.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\Wladyslaw\AppData\Roaming\npm-cache\_logs\2019-10-17T19_35_58_442Z-debug.log

Now I can't even run 'npm -v', it somehow removed my npm folder and just showing me this message:

bash: /c/Users/Wladyslaw/AppData/Roaming/npm/npm: No such file or directory


Is there any way to repair such damaged npm installation without reinstalling whole Node.js?
Thank you

2 Answers

You have just run the command line as administrator

Solution

Though it may be possible to try to "fix" your setup, it's not really worth it, you however don't want to lose your installed packages I presume. So:

  • Make a temp folder C:\Users\Wladyslaw\AppData\Roaming\npm.temp
  • Move C:\Users\Wladyslaw\AppData\Roaming\npm\node_modules to <temp_folder>
  • Move C:\Users\Wladyslaw\AppData\Roaming\npm\*.bat to <temp_folder>
  • Move C:\Users\Wladyslaw\AppData\Roaming\npm\*.cmd to <temp_folder>
  • Install node from installer (https://nodejs.org/en/download/)
  • Move back each folder and file from <temp_folder> into C:\Users\Wladyslaw\AppData\Roaming\npm
    • Don't overwrite any files!

Powershell Scripts

Haven't tested it since I have a non-traditional installation using nvm-windows. But these should do what I wrote above, so here goes:

backup.ps1

pushd $Env:AppData
$NF = 'npm.temp'
md $NF -ea SilentlyContinue | Out-Null # Make folder, if exists, don't cry about it
mv npm/node_modules $NF                # Back up node modules
$GlobalPacks = ls npm | ? Name -Match '\.(cmd|bat)$|\.npmrc' # Only select bat / cmd files or your .npmrc file
cp $GlobalPacks $NF
Write-Host -f 2 '[+] Backed up data to $NF'

Now install Node.js from the website, then run:

restore.ps1

pushd $Env:AppData
$NF = 'npm.temp'
if (!(Test-Path $NF)) {
  Write-Host -f Red "[-] Could not find backup folder, are you sure you backed up?"
  exit
}
cp npm/node_modules $NF -re -fo   # Overwrite backup modules with new files
if (!$?) {Write-Host -f yellow "[!] Something failed";exit}
rm npm/node_modules
mv "$NF/node_modules" npm         # Bring all node_modules to npm
ls $NF -File | % {mv $_ npm}      # Move global package scripts
Write-Host -f 2 '[+] Restored from [$NF] -> [$pwd\npm]'
Related