在公司防火墙后面使用 create-react-app

Use create-react-app behind corporate firewall

有任何方法可以通过设置代理在公司防火墙后使用 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.

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

更新: 看起来这个错误有 now been fixed, and should no longer be present in version 1.1.0.


此问题是由于 yarn 注册表在反应脚本中被硬编码为 registry.yarnpkg.com: https://github.com/facebookincubator/create-react-app/blob/master/packages/create-react-app/createReactApp.js#L692

根据 this pull request, if the yarn registry is not reachable, then it will check 查看您是否设置了 https_proxy 环境变量,如果是,它将验证 that 服务器是否可以访问,而不是硬编码的纱线注册表。

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

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

几周来我一直在为此苦苦挣扎。对我有用的是在命令末尾包含 --use-npm,最后我摆脱了 "no lockfile found" 错误。我真诚地希望这可以帮助其他人解决同样的问题。

我也遇到过这个问题,我用这个命令解决了这个问题:

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

这解决了我的问题 - See details npm config set registry "https://registry.npmjs.org"

这些设置帮我解决了这个问题:

npm config set proxy http://username:password@host:port
npm config set https-proxy http://username:password@host:port