How to Fix 'Invalid options object. Dev Server' Error When Adding Proxy to package.json (allowedHosts[0] Non-Empty String Issue)
If you’ve ever tried setting up a proxy in your package.json file to bypass CORS (Cross-Origin Resource Sharing) issues during development, you might have encountered a frustrating error: "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."
This error is common in modern JavaScript projects (e.g., React, Vue, or vanilla webpack setups) and often stems from misconfigurations in your development server (dev server) settings, particularly around the proxy and allowedHosts options. In this blog, we’ll break down why this error occurs, walk through step-by-step solutions to fix it, and share preventive measures to avoid it in the future.
Table of Contents#
- Understanding the Error
- Common Scenarios Triggering the Error
- Step-by-Step Solutions
- Preventive Measures
- Conclusion
- References
Understanding the Error#
Let’s start by decoding the error message:
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.
What’s Happening?#
This error is thrown by webpack-dev-server (or a tool built on it, like Create React App, Vite, or Vue CLI) when it detects invalid configuration options. The key issue here is allowedHosts[0]: the first entry in the allowedHosts array is either empty, null, undefined, or not a string.
What is allowedHosts?#
allowedHosts is a security feature in webpack-dev-server that restricts which hostnames the dev server will respond to. By default, it’s set to ['localhost', '127.0.0.1'] to prevent DNS rebinding attacks. If misconfigured (e.g., set to an empty array or non-string values), the dev server fails to start, triggering the error.
Common Scenarios Triggering the Error#
The error typically arises in two main contexts:
1. Misconfigured proxy in package.json#
Many tools (like Create React App) let you set a simple proxy in package.json to forward API requests to a backend server (e.g., http://localhost:5000). However:
- If you use an object instead of a string for
proxy(e.g., to add advanced options likechangeOrigin), some tools may misinterpret this and overrideallowedHostsincorrectly. - Typos or invalid values in the
proxyURL (e.g., trailing slashes, missinghttp://) can also cascade intoallowedHostsissues.
2. Explicitly Invalid allowedHosts in Config Files#
If you’ve customized your dev server config (e.g., in webpack.config.js, vite.config.js, or vue.config.js), setting allowedHosts to an invalid value will trigger the error. Examples of invalid values:
allowedHosts: [](empty array)allowedHosts: [null](non-string entry)allowedHosts: [''](empty string entry)
3. Outdated Dependencies#
Older versions of webpack-dev-server (pre-v4) or tools like Create React App may have bugs where proxy/allowedHosts logic is broken. For example, Create React App v4.x had issues with object-based proxy configurations conflicting with allowedHosts.
Step-by-Step Solutions#
Let’s fix the error by addressing each scenario.
Scenario 1: Misconfigured proxy in package.json#
Problem: Using an Object for proxy in Tools That Don’t Support It#
Tools like Create React App (CRA) only support string values for proxy in package.json for simple setups. If you try to use an object (e.g., to add changeOrigin or pathRewrite), CRA may misinterpret the config and corrupt allowedHosts.
Incorrect package.json Example:
{
"name": "my-app",
"proxy": { // ❌ CRA does not support object syntax here
"target": "http://localhost:5000",
"changeOrigin": true
}
} Solution: Use setupProxy.js for Advanced Proxy Config#
For CRA and similar tools, use a setupProxy.js file in your src/ directory to define advanced proxy rules. This bypasses the package.json limitations.
Step 1: Install http-proxy-middleware (required for setupProxy.js):
npm install http-proxy-middleware --save-dev Step 2: Create src/setupProxy.js:
const { createProxyMiddleware } = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/api', // Forward requests starting with /api
createProxyMiddleware({
target: 'http://localhost:5000', // Backend server URL
changeOrigin: true, // Needed for virtual hosted sites
pathRewrite: { '^/api': '' } // Optional: strip /api from the URL
})
);
}; Step 3: Remove the object-based proxy from package.json (or set it to a string if using a simple proxy):
{
"name": "my-app",
"proxy": "http://localhost:5000" // ✅ Valid string syntax (for simple cases)
} Scenario 2: Invalid allowedHosts in Dev Server Config#
If you’ve explicitly configured allowedHosts in a dev server config file (e.g., webpack.config.js, vite.config.js), ensure it’s an array of non-empty strings.
Example: Fixing webpack.config.js#
If you’re using a custom webpack.config.js, check the devServer section:
Incorrect Config:
// webpack.config.js
module.exports = {
devServer: {
allowedHosts: [''] // ❌ Empty string in allowedHosts
}
}; Correct Config:
// webpack.config.js
module.exports = {
devServer: {
allowedHosts: ['localhost', '127.0.0.1', 'myapp.local'] // ✅ Non-empty strings
// OR use 'all' to allow all hosts (not recommended for production)
// allowedHosts: 'all'
}
}; Example: Fixing vite.config.js (Vite Users)#
Vite uses server.allowedHosts (similar to webpack):
Incorrect Config:
// vite.config.js
export default defineConfig({
server: {
allowedHosts: [] // ❌ Empty array
}
}); Correct Config:
// vite.config.js
export default defineConfig({
server: {
allowedHosts: ['localhost', '127.0.0.1'] // ✅ Valid hosts
}
}); Scenario 3: Outdated Dependencies#
Older versions of webpack-dev-server (e.g., v3.x) or tools like Create React App may have bugs that cause allowedHosts misconfigurations. Updating dependencies often resolves these issues.
How to Update:#
-
For Create React App:
# Update to the latest version npx create-react-app@latest my-app --template typescript # Or your existing template # Or upgrade dependencies in an existing project npm install react-scripts@latest -
For Direct
webpack-dev-serverUsers:npm install webpack-dev-server@latest --save-dev
Preventive Measures#
To avoid this error in the future:
-
Stick to Tool-Specific Proxy Guidelines
- Use
package.jsonproxyonly for strings (simple setups). - For advanced proxies, use tool-specific config files (e.g.,
setupProxy.jsfor CRA,vite.config.jsfor Vite).
- Use
-
Validate
allowedHosts- Always ensure
allowedHostsis an array of non-empty strings (e.g.,['localhost', '127.0.0.1']). - Avoid
allowedHosts: 'all'in production (it disables the security check).
- Always ensure
-
Keep Dependencies Updated
Regularly updatewebpack-dev-server,react-scripts, Vite, or other dev tools to patch bugs. -
Test Config Changes Incrementally
When adding a proxy or modifyingallowedHosts, test the dev server after each change to catch issues early.
Conclusion#
The "Invalid options object. Dev Server" error with allowedHosts[0] is a configuration issue, not a bug. It occurs when allowedHosts is misconfigured (e.g., empty strings, invalid arrays) or when proxy settings conflict with dev server rules.
By:
- Using the correct
proxysyntax for your tool (string for simple setups,setupProxy.jsfor advanced), - Ensuring
allowedHostscontains non-empty strings, - Updating dependencies,
you can resolve the error and get your dev server running smoothly.