question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

In CRA 5.0.0, `react-scripts start` fails when both `HOST` in `.env` and `proxy` in `package.json` are defined.

See original GitHub issue

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

  1. Create a new app.
  2. Set HOST in .env.
  3. Set proxy in package.json.
  4. 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

https://github.com/BalzGuenat/cra-test

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:11
  • Comments:16 (1 by maintainers)

github_iconTop GitHub Comments

19reactions
sunmoon-idegucommented, Dec 27, 2021

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.

  1. Delete “proxy”: “http://localhost:xxxx” in package.json
  2. run npm install http-proxy-middleware
  3. Create a file setupProxy.js inside your src folder and write the following codes:
const { createProxyMiddleware } = require('http-proxy-middleware');

module.exports = function(app) {
  app.use(
    '/api',
    createProxyMiddleware({
      target: 'http://localhost:6000',
      changeOrigin: true,
    })
  );
};
  1. run npm start
11reactions
hwinklercommented, Dec 28, 2021

I encountered the problem when I tried:

HOST=<something>  yarn start

(and I have a proxy: setting in package.json).

I debugged a bit and found that in webpackDevServer.config.js#L46, allowedHosts is set to [undefined] because prepareUrls doesn’t set lanUrlForConfig if a host is specified.

By examining the code at that link I discovered the workaround:

HOST=<something>  DANGEROUSLY_DISABLE_HOST_CHECK=true  yarn start

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Invalid options object. Dev Server has been initialized using ...
When trying to emulate what I was using with the proxy value in my package.json file, I receive a client error: domain option...
Read more >
What Does Create-React-App Actually Do? | by Andrew Mc
This is where the line “Installing react, react-dom, and react-scripts…” appears. The installation is done asynchronously. If it fails package.json, ...
Read more >
react-app-rewired - npm
Start using react-app-rewired in your project by running `npm i react-app-rewired`. There are 315 other projects in the npm registry using ...
Read more >
Why you should use a proxy server with Create React App
Because most client apps are hosted on different servers than the backend application, there are complications that arise as a result of ...
Read more >
Which changes are necessary for upgrading to React 18 and ...
This is my package.json file after performing upgrade. ... "test": "cross-env CI=true react-scripts test --env=jsdom", ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found