To resolve the error "Invalid options object. Dev Server has been initialized using an options object that does not match the API schema," follow these steps:
Here is a workaround:
Delete the line "proxy": "http://localhost:6000"
from your configuration.
Then, install the package http-proxy-middleware
using the command npm install http-proxy-middleware --save
.
Create a file named setupProxy.js
inside your src
folder or at the root of your project directory.
Add the following lines inside setupProxy.js
:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/api',
createProxyMiddleware({
target: 'http://localhost:6000',
changeOrigin: true,
})
);
}
The other solutions didn't work for me, so here's what I found:
This seems to be a Create React App (CRA) bug or possibly a security feature, where allowedHosts
gets set to [undefined]
because prepareUrls
doesn't set lanUrlForConfig
when both a host and a proxy are specified. You can find the relevant CRA GitHub issue here.
If appropriate for your use case (learn more here), this issue can be avoided by creating a .env
file and adding DANGEROUSLY_DISABLE_HOST_CHECK=true
to it, or by trying DANGEROUSLY_DISABLE_HOST_CHECK=true yarn start
.
Setting "allowedHosts"
as a property in package.json
didn't work for me. I had to wrap it in an options
property:
"options": {
"allowedHosts": ["localhost", ".localhost"],
"proxy": "https://localhost:3386/"
}
After that, running npm start
did the trick without having to reinstall modules.
If you add or remove "proxy": "http://localhost:6000"
in package.json
, then remove node_modules
and reinstall it. This works for me.
I faced the same issue several times, but finally, I found a solution!
I solved this problem by adding "allowedHosts": "all"
to my package.json
file.
Example:
"allowedHosts": "all",
"proxy": "http://localhost:3001/api",
"name": "simple-blog",
"version": "0.1.0",
"private": true,
"dependencies": {
"@testing-library/jest-dom": "^5.1"
}