How to change port number in vue-cli project

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


Solution 1:

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/

Solution 2:

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:

  • https://cli.vuejs.org/config/#devserver
  • https://cli.vuejs.org/config/#vue-config-js
  • https://cli.vuejs.org/guide/mode-and-env.html

Vue CLI v2 (deprecated)

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

References:

  • http://vuejs-templates.github.io/webpack/
  • https://github.com/vuejs-templates/webpack/blob/develop/template/build/webpack.dev.conf.js#L35