【问题标题】:"npm install -g npm@latest" corrupted my npm installation - Windows 8.1“npm install -g npm@latest”损坏了我的 npm 安装 - Windows 8.1
【发布时间】:2020-04-05 14:35:09
【问题描述】:

上个月我安装了 node 10.16.3 LTS,因为它没有最新的 npm 版本,所以我使用这个官方指南更新了它:https://docs.npmjs.com/try-the-latest-stable-version-of-npm(选项 2):

  1. 删除了 %ProgramFiles%\nodejs\npm%ProgramFiles%\nodejs\npm.cmd
  2. 将“npmrc”文件从 %ProgramFiles%\nodejs\node_modules\npm 移动到 %appdata%\npm\node_modules\npm


今天,在我安装了“create-react-app”之后,npm 向我显示了 npm 的新次要版本可用 6.11.3 -> 6.12.0 并且我可以更新它的消息。
我运行了“npm install -g npm@latest”,但这只是给我带来了一堆错误:

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

现在我什至无法运行“npm -v”,它以某种方式删除了我的 npm 文件夹并只显示这条消息:

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


有没有办法在不重新安装整个 Node.js 的情况下修复这种损坏的 npm 安装?
谢谢

【问题讨论】:

  • 所以我不得不完全重新安装 Node.js。显然上面提到的指南有些过时了。最新的节点安装程序根本没有将 'C:\Program Files (x86)\nodejs' 放在 PATH 上,只有 'C:\Users\\AppData\Roaming\npm'。刚刚运行相同的“npm install -g npm@latest”,现在我有了最新的 npm

标签: node.js windows npm


【解决方案1】:

你刚刚以管理员身份运行命令行

【讨论】:

    【解决方案2】:

    解决方案

    虽然有可能尝试“修复”您的设置,但这并不值得,但我认为您不想丢失已安装的软件包。所以:

    • 创建一个临时文件夹C:\Users\Wladyslaw\AppData\Roaming\npm.temp
    • C:\Users\Wladyslaw\AppData\Roaming\npm\node_modules 移动到<temp_folder>
    • C:\Users\Wladyslaw\AppData\Roaming\npm\*.bat 移动到<temp_folder>
    • C:\Users\Wladyslaw\AppData\Roaming\npm\*.cmd 移动到<temp_folder>
    • 从安装程序安装节点 (https://nodejs.org/en/download/)
    • 将每个文件夹和文件从<temp_folder>移回C:\Users\Wladyslaw\AppData\Roaming\npm
      • 不要覆盖任何文件!

    Powershell 脚本

    由于我使用nvm-windows 进行了非传统安装,因此尚未对其进行测试。但是这些应该做我上面写的,所以这里是:

    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'
    

    现在从网站安装 Node.js,然后运行:

    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]'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-10-20
      • 2017-11-09
      • 1970-01-01
      • 2019-05-16
      • 1970-01-01
      • 2013-12-14
      • 1970-01-01
      相关资源
      最近更新 更多