How to change port number in vue-cli project

2020-02-07 19:15发布

How to change Port number in Vue-cli project so that it run's on another port instead of 8080.

12条回答
家丑人穷心不美
2楼-- · 2020-02-07 19:16

The port for the Vue-cli webpack template is found in your app root's myApp/config/index.js.

All you have to do is modify the port value inside the dev block:

 dev: {
    proxyTable: {},
    env: require('./dev.env'),
    port: 4545,
    assetsSubDirectory: 'static',
    assetsPublicPath: '/',
    cssSourceMap: false
  }

Now you can access your app with localhost:4545

also if you have .env file better to set it from there

查看更多
孤傲高冷的网名
3楼-- · 2020-02-07 19:19

Go to node_modules/@vue/cli-service/lib/options.js
At the bottom inside the "devServer" unblock the codes
Now give your desired port number in the "port" :)

devServer: {
   open: process.platform === 'darwin',
   host: '0.0.0.0',
   port: 3000,  // default port 8080
   https: false,
   hotOnly: false,
   proxy: null, // string | Object
   before: app => {}
}
查看更多
一纸荒年 Trace。
4楼-- · 2020-02-07 19:20

Best way is to update the serve script command in your package.json file. Just append --port 3000 like so:

"scripts": {
  "serve": "vue-cli-service serve --port 3000",
  "build": "vue-cli-service build",
  "inspect": "vue-cli-service inspect",
  "lint": "vue-cli-service lint"
},
查看更多
地球回转人心会变
5楼-- · 2020-02-07 19:21

If you're using vue-cli 3.x, you can simply pass the port to the npm command like so:

npm run serve -- --port 3000

Then visit http://localhost:3000/

查看更多
冷血范
6楼-- · 2020-02-07 19:22

Late to the party, but I think it's helpful to consolidate all these answers into one outlining all options.

Separated in Vue CLI v2 (webpack template) and Vue CLI v3, ordered by precedence (high to low).

Vue CLI v3

  • package.json: Add port option to serve script: scripts.serve=vue-cli-service serve --port 4000
  • CLI Option --port to npm run serve, e.g. npm run serve -- --port 3000. Note the --, this makes passes the port option to the npm script instead of to npm itself. Since at least v3.4.1, it should be e.g. vue-cli-service serve --port 3000.
  • Environment Variable $PORT, e.g. PORT=3000 npm run serve
  • .env Files, more specific envs override less specific ones, e.g. PORT=3242
  • vue.config.js, devServer.port, e.g. devServer: { port: 9999 }

References:

Vue CLI v2 (deprecated)

  • Environment Variable $PORT, e.g. PORT=3000 npm run dev
  • /config/index.js: dev.port

References:

查看更多
别忘想泡老子
7楼-- · 2020-02-07 19:22

An alternative approach with vue-cli version 3 is to add a .env file in the root project directory (along side package.json) with the contents:

PORT=3000

Running npm run serve will now indicate the app is running on port 3000.

查看更多
登录 后发表回答