You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置Webpack使React运行在指定端口8017?

不需要修改package.json,只需调整webpack.config.js

你完全不用改动package.json里的内容,只需要把找到的devServer配置添加到你的webpack.config.js中,就能实现将React运行在8017端口的需求。

修改后的webpack.config.js代码如下:

const path = require('path'); // 确保顶部引入path模块,原代码用到了path.resolve,这个不能少

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

原因很直接:你package.json里的start命令用的是webpack serve,这个命令会自动读取webpack.config.js中的devServer配置项,所以把端口配置写在webpack.config.js里就会生效,不需要在package.json的命令里额外添加端口参数。

内容的提问来源于stack exchange,提问作者fransua

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 19:15:55