在企业防火墙后面使用 create-react-app

IT技术 node.js reactjs
2021-03-25 06:34:24

create-react-app通过设置代理,可以通过任何方式在企业防火墙后面工作

我已经在 npm 和 yarn 上设置了代理,但这是我在尝试时看到的create-react-app my-app

Creating a new React app in F:\react\my-app.

Installing packages. This might take a couple of minutes.
Installing react, react-dom, and react-scripts...

You appear to be offline.
Falling back to the local Yarn cache.

yarn add v0.23.4
info No lockfile found.
[1/4] Resolving packages...
error Couldn't find any versions for "react" that matches "latest" in our cache. Possible versions: ""
info Visit https://yarnpkg.com/en/docs/cli/add for documentation about this command.

Aborting installation.
  yarnpkg add --exact --offline react react-dom react-scripts has failed.

Deleting generated file... package.json
Deleting my-app / from F:\react
Done.
6个回答

我已经为此苦苦挣扎了几个星期。对我有用的是在命令末尾包含 --use-npm ,最后我摆脱了“未找到锁定文件”错误。我真诚地希望这可以帮助其他人在同样的问题上挣扎。

谢谢埃里克!
2021-06-10 06:34:24
--use-npm工作对我来说是良好,strict-ssl=falsealways-auth=true_authca
2021-06-12 06:34:24
完美的作品谢谢。我的公司代理有点烦人,所以这个修复很简单有效。
2021-06-14 06:34:24
嗨 Erik,--use-npm 完美运行,感谢分享您的解决方案!
2021-06-19 06:34:24

我也有这个问题,我用这个命令解决了这个问题:

create-react-app hello-world --use-npm --cache /tmp/empty-cache

作为解决方法,首先尝试yarn add react react-dom react-scripts在空文件夹中运行这会将所需的包添加到纱线的本地缓存中。然后您可以删除该文件夹并create-react-app my-app再次运行,它有望工作。

更新:看起来这个错误现在已经被修复,并且应该不再出现在1.1.0 版本中


此问题是由于纱线注册表在 react-scripts 中硬编码为registry.yarnpkg.comhttps : //github.com/facebookincubator/create-react-app/blob/master/packages/create-react-app/createReactApp .js#L692

根据此拉取请求,如果无法访问 yarn 注册表,则它会检查您是否https_proxy设置环境变量,如果设置了,它将验证服务器是否可访问,而不是硬编码的 yarn 注册表。

重申一下,在 npm/yarn 中设置代理/https-proxy 配置是不够的 - 您还需要设置https_proxy环境变量,因为这就是 create-react-app 在确定您是否在线时将检查的内容。例如:

https_proxy=https://my.corporate.proxy create-react-app my-new-project

这解决了我的问题 -查看详情 npm config set registry "https://registry.npmjs.org"