17

当我尝试在我的 Windows 10 上安装 react 应用程序时遇到了一些问题npx create-react-app appname,收到以下错误消息:

Error: EPERM: operation not permitted, mkdir 'C:\Users\Daniel'
TypeError: Cannot read property 'loaded' of undefined
    at exit (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:98:27)
    at errorHandler (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:216:3)
    at C:\Program Files\nodejs\node_modules\npm\bin\npm-cli.js:77:20
    at cb (C:\Program Files\nodejs\node_modules\npm\lib\npm.js:225:22)
    at C:\Program Files\nodejs\node_modules\npm\lib\npm.js:263:24
    at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:83:7
    at Array.forEach (<anonymous>)
    at C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:82:13
    at f (C:\Program Files\nodejs\node_modules\npm\node_modules\once\once.js:25:25)
    at afterExtras (C:\Program Files\nodejs\node_modules\npm\lib\config\core.js:173:20)
C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:98
  var doExit = npm.config.loaded ? npm.config.get('_exit') : true
                          ^

TypeError: Cannot read property 'loaded' of undefined
    at exit (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:98:27)
    at process.errorHandler (C:\Program Files\nodejs\node_modules\npm\lib\utils\error-handler.js:216:3)
    at process.emit (events.js:198:13)
    at process._fatalException (internal/bootstrap/node.js:496:27)
Install for create-react-app@latest failed with code 7

解决方案

我开始了一些研究,最后发现,如果您的 Windows 用户名包含空格,例如“C:\Users\Firstname Lastname\AppData\Roaming\npm-cache”,就会出现此问题

经过几个小时的循环运行并在这个问题上重新阅读多个 GitHub 线程后,我终于找到了一个对我有用的补丁:npm config set cache "C:\Users\Firstname~1\AppData\Roaming\npm-cache" --global从你的终端运行,它将用 ~1 替换空格和后面的所有内容,这似乎解决了这个问题

我发现这个解决方案的来源是https://github.com/zkat/npx/issues/146#issuecomment-384019497

我在此处发布此评论是为了节省任何人处理大量循环链接的 github 线程的苦差事。

4

4 回答 4

32

解决方案

如果要使用用户名中有空格的当前路径,"C:\Users\Firstname Lastname\AppData\Roaming\npm-cache" 可以将空格后的字符串替换为"~1"

npm config set cache "C:\Users\Firstname~1\AppData\Roaming\npm-cache" --global

于 2019-10-12T13:25:07.737 回答
18

如果您仍然遇到此问题,可以尝试以下操作:

我可以通过在我的用户文件夹中创建一个目录连接来解决这个问题,该文件夹中没有空间。您可以在管理 powershell 中运行这样的命令:

cmd /c mklink /J "C:\Users\myname" "C:\Users\My Name"

然后,您可以使用此联结,就好像它是您的真实用户目录一样:

npm config set cache C:\Users\myname\AppData\Roaming\npm-cache

npm config set prefix C:\Users\myname\AppData\Roaming\npm

(上面应该更新您的用户 .npmrc 文件)

来源

于 2020-03-17T07:49:35.577 回答
0

这是我解决这个问题的方法:

  1. 从中删除 npm 文件夹%APPDATA%
  2. Set both prefix and cache to paths without spaces. Be aware that using prefix and cache paths out of your user's folder may expose your system to security risks.
    npm config set prefix F:\PathWithoutSpace\
    npm config set prefix F:\PathWithoutSpace\ --global
    npm config set cache F:\PathWithoutSpace\cache
    npm config set cache F:\PathWithoutSpace\cache --global
  1. Re-install packages like yarn and creat-react-app
  2. Add F:\PathWithoutSpace\ to your User's Path environment variable, which already includes an expanded version of %APPDATA%\npm, which will no longer be required.

I think it will be worth notifying the user of these problems when they install the NodeJS.

于 2021-02-03T19:15:29.403 回答
0

I did update the node version to 16.13.2 which solved the problem.

于 2022-02-08T12:42:04.240 回答