create-react-app: In CRA 5.0.0, `react-scripts start` fails when both `HOST` in `.env` and `proxy` in `package.json` are defined.
Describe the bug
In CRA 5.0.0, react-scripts start
fails when both HOST
in .env
and proxy
in package.json
are defined.
Did you try recovering your dependencies?
This occurs on a newly created project.
Which terms did you search for in User Guide?
“allowedHosts” and others, came up empty.
Environment
Environment Info:
current version of create-react-app: 5.0.0
running from /home/z003w3we/.npm/_npx/c67e74de0542c87c/node_modules/create-react-app
System:
OS: Linux 5.4 Ubuntu 20.04.3 LTS (Focal Fossa)
CPU: (4) x64 Intel(R) Core(TM) i7-9850H CPU @ 2.60GHz
Binaries:
Node: 16.13.0 - ~/.asdf/installs/nodejs/16.13.0/bin/node
Yarn: 1.22.10 - ~/.asdf/shims/yarn
npm: 8.1.3 - ~/.asdf/plugins/nodejs/shims/npm
Browsers:
Chrome: 96.0.4664.45
Firefox: 95.0
npmPackages:
react: ^17.0.2 => 17.0.2
react-dom: ^17.0.2 => 17.0.2
react-scripts: 5.0.0 => 5.0.0
npmGlobalPackages:
create-react-app: Not Found
Steps to reproduce
- Create a new app.
- Set
HOST
in.env
. - Set
proxy
inpackage.json
. - Run
react-scripts start
or something to that effect.
Expected behavior
App starts.
Actual behavior
I get this error:
> yarn start
yarn run v1.22.10
$ react-scripts start
Attempting to bind to HOST environment variable: test.localhost
If this was unintentional, check that you haven't mistakenly set it in your shell.
Learn more here: https://cra.link/advanced-config
Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options.allowedHosts[0] should be a non-empty string.
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
Reproducible demo
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 14
- Comments: 19 (1 by maintainers)
Commits related to this issue
- fix(ui): 🐛 workaround cra proxy bug ref https://github.com/facebook/create-react-app/issues/11762\#issuecomment-1001295074 — committed to collinbarrett/FilterLists by collinbarrett 2 years ago
- fix(ui): 🐛 workaround cra proxy bug ref https://github.com/facebook/create-react-app/issues/11762\#issuecomment-1001295074 — committed to collinbarrett/FilterLists by collinbarrett 2 years ago
- fix(ui): 🐛 workaround cra proxy bug ref https://github.com/facebook/create-react-app/issues/11762\#issuecomment-1001295074 — committed to collinbarrett/FilterLists by collinbarrett 2 years ago
- fix(ui): 🐛 workaround cra proxy bug ref https://github.com/facebook/create-react-app/issues/11762\#issuecomment-1001295074 — committed to collinbarrett/FilterLists by collinbarrett 2 years ago
- fix: 跨域代理 bug 解决 参考资料,github-facebook issue:https: //github.com/facebook/create-react-app/issues/11762 StackOverflow:https://stackoverflow.com/questions/70374005/invalid-options-object-dev-server-has... — committed to painting-alt/tigerZoo by hua-qi 2 years ago
- create-react-app v5 最初動かなかったので @craco/craco をremove (v5に対応してなかった) これを参考にproxyの設定を変えた https://github.com/facebook/create-react-app/issues/11762 — committed to mieze018/mieze018.github.io by mieze018 2 years ago
- 🧰chore: create-react-app v5 最初動かなかったので @craco/craco をremove (v5に対応してなかった) これを参考にproxyの設定を変えた https://github.com/facebook/create-react-app/issues/11762 — committed to mieze018/mieze018.github.io by mieze018 2 years ago
- :heavy_plus_sign: Add: 프록시 미들웨어 추가 - React-Script 5.0에서 package.json 파일에 proxy 속성 추가 시 오류 발생 - 참고 이슈 : https://github.com/facebook/create-react-app/issues/11762 — committed to romantech/shopping-insight by romantech 2 years ago
- https://github.com/facebook/create-react-app/issues/11762 — committed to vzhyhunou/vzh-cms by deleted user a year ago
The answer from josipat in
https://stackoverflow.com/questions/70374005/invalid-options-object-dev-server-has-been-initialized-using-an-options-object/70491173#70491173
works. I will just paste his answer here again, just for easy access.
npm install http-proxy-middleware
npm start
I encountered the problem when I tried:
(and I have a
proxy:
setting inpackage.json
).By examining the code at that link I discovered the workaround:
Before you try that you should read the comment following
webpackDevServer.config.js#L25
and determine whether that is dangerous for your situation. In my case it’s harmless.@sunmoon-idegu I’m not sure how important it is, but the syntax of the above snippet is a little misleading. I think what the intention was to create a proxy on the /api routes, rather than the / routes. I noticed this because the output of the log was saying:
Instead of:
So, to fix this you will notice the path (HPM’s context) is not being set, therefore defaulting to /. You can correctly set via:
Also, saving someone a doc lookup, you will likely need
secure: false
if testing locally and receiving SSL errors.I debugged a bit and found that in
webpackDevServer.config.js#L46
,allowedHosts
is set to[undefined]
becauseprepareUrls
doesn’t setlanUrlForConfig
if a host is specified.Yeah, it works in webpack 4.44.2.
thank you its working properly
I’d same problem but it fixed by setupProxy.js
Glad you found a workaround Brett. Just to clear up why your # issue. Those are client only elements of the URL and do not make it to the server. I forget where I read that but it would explain your observations.
On Wed, Apr 20, 2022 at 8:36 AM Brett Pittman @.***> wrote: