【问题标题】:React / Linux / How to configure webpack to run React on specific port?React / Linux / 如何配置 webpack 在特定端口上运行 React?
【发布时间】:2022-10-14 18:11:32
【问题描述】:

这是我的 package.json :

  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "webpack serve  --hot --open",
    "build": "webpack --config webpack.config.js --mode production"
  },

这是我的 webpack.config.js

module.exports = {
  entry: "./src/index.js",
  output: {
    filename: "bundle.[hash].js",
    path: path.resolve(__dirname, "dist"),
  },

我想在端口 8017 上运行 react。

我在 SO 中找到了solution,说我应该将这些行添加到我的 webpack 中:

devServer: {
    inline:true,
    port: 8017
  },

这是否意味着我需要在我的 package.json 中添加一个新行?

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    只需在 webpack.config.js 中添加适当的参数。查看 webpack 的官方文档 (https://webpack.js.org/configuration/dev-server/#devserver)。它应该如下所示:

    module.exports = {
      entry: "./src/index.js",
      devServer: {
      ...your custom devServer config,
      port: 8017,
      },
      output: {
        filename: "bundle.[hash].js",
        path: path.resolve(__dirname, "dist"),
      },
    };
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-04-02
      • 2012-07-19
      • 1970-01-01
      • 2017-06-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多