如何在Vue-cli项目中更改端口号,使其在另一个端口上运行而不是8080。


当前回答

最好的方法是更新包中的serve脚本命令。json文件。只需像这样追加——port 3000:

"scripts": {
  "serve": "vue-cli-service serve --port 3000",
  "build": "vue-cli-service build",
  "inspect": "vue-cli-service inspect",
  "lint": "vue-cli-service lint"
},

其他回答

将PORT环境变量添加到package.json中的服务脚本中:

"serve": "PORT=4767 vue-cli-service serve",

在webpack.config.js中:

module.exports = {
  ......
  devServer: {
    historyApiFallback: true,
    port: 8081,   // you can change the port there
    noInfo: true,
    overlay: true
  },
  ......
}

您可以在模块中更改端口。exports -> devServer ->端口。

然后你重新启动npm run dev。你可以得到那个。

你应该擅长这个:

“serve”:“vue-cli-service service——port 8081”,

如果你想改变localhost端口,你可以改变package.json中的scripts标签:

 "scripts": {
    "serve": "vue-cli-service serve --port 3000",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },

vue-cli版本3的另一种方法是在根项目目录中添加一个.env文件(与package.json一起),内容如下:

端口= 3000

运行npm run serve现在将指示应用程序在端口3000上运行。