Dev Server has been initialized using an options object that does not match the API schema. – options has an unknown property 'public'

huangapple go评论66阅读模式
英文:

Dev Server has been initialized using an options object that does not match the API schema. - options has an unknown property 'public'

问题

出现了将Django与Vue.js链接的错误。

在我的vue.config.js文件中:

const BundleTracker = require('webpack-bundle-tracker');

module.exports = {
  publicPath: 'http://127.0.0.1:8080/',
  outputDir: './dist/',
  
  chainWebpack: config => {
    config.optimization.splitChunks(false);

    config.plugin('BundleTracker').use(BundleTracker, [{ filename: './webpack-stats.json' }]);

    config.devServer
      .public('http://0.0.0.0:8080')
      .host('0.0.0.0')
      .port(8080)
      .https(false)
      .headers({ "Access-Control-Allow-Origin": ["*"] });
  },

  pages: {
    index: 'src/main.js'
  }
}

这是在命令行中的错误。

npm run serve

> django-vue@0.1.0 serve
> vue-cli-service serve

 INFO  Starting development server...
 ERROR  ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
         - options has an unknown property 'public'. These properties are valid:
           object { allowedHosts?, bonjour?, client?, compress?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, magicHtml?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, server?, setupExitSignals?, setupMiddlewares?, static?, watchFiles?, webSocketServer? }
ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
 - options has an unknown property 'public'. These properties are valid:
   object { allowedHosts?, bonjour?, client?, compress?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, magicHtml?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, server?, setupExitSignals?, setupMiddlewares?, static?, watchFiles?, webSocketServer? }
    at validate (/Users/byengju/Documents/01. Project/dataglass/django-vue/node_modules/schema-utils/dist/validate.js:115:11)
    at new Server (/Users/byengju/Documents/01. Project/dataglass/django-vue/node_modules/webpack-dev-server/lib/Server.js:231:5)
    at serve (/Users/byengju/Documents/01. Project/dataglass/django-vue/node_modules/@vue/cli-service/lib/commands/serve.js:194:20)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

我该如何解决这个问题?

英文:

There was an error linking django with vuejs.

in my vue.config.js file:

const BundleTracker = require('webpack-bundle-tracker');

module.exports = {
  publicPath: 'http://127.0.0.1:8080/',
  outputDir: './dist/',

  chainWebpack: config => {
    config.optimization.splitChunks(false)

    config.plugin('BundleTracker').use(BundleTracker, [{filename: './webpack-stats.json'}])

    config.devServer.public('http://0.0.0.0:8080').host('0.0.0.0').port(8080).https(false).headers({"Access-Control-Allow-Origin":["\*"]})
  },

  pages: {
    index: 'src/main.js'
  }
}

This is an error on cmd.

npm run serve

> django-vue@0.1.0 serve
> vue-cli-service serve

INFO Starting development server...
ERROR ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.
- options has an unknown property 'public'. These properties are valid:
object { allowedHosts?, bonjour?, client?, compress?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, magicHtml?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, server?, setupExitSignals?, setupMiddlewares?, static?, watchFiles?, webSocketServer? }
ValidationError: Invalid options object. Dev Server has been initialized using an options object that does not match the API schema.

  • options has an unknown property 'public'. These properties are valid:
    object { allowedHosts?, bonjour?, client?, compress?, devMiddleware?, headers?, historyApiFallback?, host?, hot?, http2?, https?, ipc?, liveReload?, magicHtml?, onAfterSetupMiddleware?, onBeforeSetupMiddleware?, onListening?, open?, port?, proxy?, server?, setupExitSignals?, setupMiddlewares?, static?, watchFiles?, webSocketServer? }
    at validate (/Users/byengju/Documents/01. Project/dataglass/django-vue/node_modules/schema-utils/dist/validate.js:115:11)
    at new Server (/Users/byengju/Documents/01. Project/dataglass/django-vue/node_modules/webpack-dev-server/lib/Server.js:231:5)
    at serve (/Users/byengju/Documents/01. Project/dataglass/django-vue/node_modules/@vue/cli-service/lib/commands/serve.js:194:20)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

How do I solve it?

答案1

得分: 1

正如错误提示所述,public 在你的代码中不是一个有效的属性,你使用了 config.devServer.public('http://0.0.0.0:8080')publicwebpack-dev-server v3中的属性,但根据你的错误信息和可用属性名称列表,显然你正在使用webpack-dev-server v4

不过,你需要问自己是否需要指定通常作为默认 devserver 地址的 0.0.0.0:8080(localhost:8080)。尝试移除该属性并测试你的应用是否正常工作。通常,这样的属性仅在代理或 WebSocket 地址时才需要。迁移指南 从 v3 到 v4 甚至指出:

public、sockHost、sockPath 和 sockPort 选项已被移除,以支持 client.webSocketURL 选项:

v3:

module.exports = {
  devServer: {
    public: "ws://localhost:8080",
  },
};
module.exports = {
  devServer: {
    sockHost: "0.0.0.0",
    sockPath: "/ws",
    sockPort: 8080,
  },
};

v4:

module.exports = {
  devServer: {
    client: {
      // 可以是字符串:
      //
      // 从浏览器获取协议/主机名/端口
      // webSocketURL: 'auto://0.0.0.0:0/ws';
      webSocketURL: {
        hostname: "0.0.0.0",
        pathname: "/ws",
        port: 8080,
      },
    },
  },
};
英文:

As the error states, public is not a valid property where you have config.devServer.public('http://0.0.0.0:8080'). It is a property from webpack-dev-server v3 but based on your error message and the list of available property names, you are clearly on webpack-dev-server v4.

Ask yourself though: do you need to specify what is normally the default devserver address, 0.0.0.0:8080 (localhost:8080)? Try just removing the property and test if your app works. Usually such a property is only needed for proxy or websocket addresses. The migration guide from v3 to v4 even says:

> public, sockHost, sockPath, and sockPort options were removed in favor client.webSocketURL option:

v3:

module.exports = {
  devServer: {
    public: "ws://localhost:8080",
  },
};
module.exports = {
  devServer: {
    sockHost: "0.0.0.0",
    sockPath: "/ws",
    sockPort: 8080,
  },
};

v4:

module.exports = {
  devServer: {
    client: {
      // Can be `string`:
      //
      // To get protocol/hostname/port from browser
      // webSocketURL: 'auto://0.0.0.0:0/ws'
      webSocketURL: {
        hostname: "0.0.0.0",
        pathname: "/ws",
        port: 8080,
      },
    },
  },
};

huangapple
  • 本文由 发表于 2023年2月27日 12:37:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576820.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定