How to change port number in vue-cli project
Asked Answered
C

21

222

How to change the Port number in the Vue-CLI project so that it runs on another port instead of 8080.

Compassionate answered 10/11, 2017 at 9:45 Comment(1)
On macOS, please remember about this https://mcmap.net/q/120418/-trying-to-change-port-to-80-on-webpack-dev-server-gives-errorCarrick
M
40

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

Marchioness answered 10/11, 2017 at 11:38 Comment(10)
In the latest vuejs version. The file is no longer used, instead using: <your_project_root>/vue.config.js.Grindelia
Your answer is correct, but please mention Vue version.Cantaloupe
do you mean the vue-cli version? vue version has no effect on thisMarchioness
The file myApp/config/index.js does not exist!Psychiatrist
Vue CLI 3 uses vue.config.js at project root. It has to be created manually IIRC.Daughterinlaw
there is no vue.config.js thereEndanger
sorry, I think with the latest update it has changed, you can update the answer one you figure it outMarchioness
"scripts": { "serve": "vue-cli-service serve --port 80" },Allix
Since this is the (un)helpful first listed response you also want to check: cli.vuejs.org/config/#vue-config-js under some sub section it is shown.Heaney
Also, you can use npm run serve -- --port 8085Saprogenic
P
258

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/

Privy answered 14/7, 2018 at 20:16 Comment(3)
You saved me precious time, as the first -- is not written in the doc: cli.vuejs.org/guide/cli-service.html#using-the-binary. I was typing npm run serve --port 3000 which seems logical to me, but I got errors... Thumbs up!Mantelpiece
That's because the first -- escapes the parameters given to npm run serve and not to vue-cli-service. If you edit package.json and the serve command directly, you enter it as shown in the documentation: "serve": "vue-cli-service serve --port 3000",Incessant
While this answer is good and has most up-votes (mine too ;)), I prefer package.json change in order not to have to give port to command every time but have it persistent and automatic in npm run serve command.Gracchus
K
161

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:

Keller answered 26/10, 2018 at 11:6 Comment(5)
Looks like this changed a tad in latest vue cli (using 3.4.1), here's my "serve" line in package.json: "serve": "vue-cli-service serve --port 4000",. Works great!Clipper
@Clipper Thanks, I verified it and added it to the answer.Keller
The answers above don't seem to work in v3 at this date. I tried the .env option, package.json, vue.config.js, and CLI command option but they all get ignored. The config file docs say "Some values like host, port and https may be overwritten by command line flags." cli.vuejs.org/config/#devserver Am I missing something? Anybody else having issues?Alsatia
@Alsatia - This was reported yesterday in the VueJS CLI Repository Issues: github.com/vuejs/vue-cli/issues/4452 It is saying to install portfinder (github.com/http-party/node-portfinder) as there was a breaking change which happened with that.Mosra
@Clipper this has done the trick in my case also. Thanks wwerner for the respose.Chopin
B
51

As the time of this answer's writing (May 5th 2018), vue-cli has its configuration hosted at <your_project_root>/vue.config.js. To change the port, see below:

// vue.config.js
module.exports = {
  // ...
  devServer: {
    open: process.platform === 'darwin',
    host: '0.0.0.0',
    port: 8080, // CHANGE YOUR PORT HERE!
    https: false,
    hotOnly: false,
  },
  // ...
}

Full vue.config.js reference can be found here: https://cli.vuejs.org/config/#global-cli-config

Note that as stated in the docs, “All options for webpack-dev-server” (https://webpack.js.org/configuration/dev-server/) is available within the devServer section.

Bouchard answered 5/5, 2018 at 13:13 Comment(0)
M
40

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

Marchioness answered 10/11, 2017 at 11:38 Comment(10)
In the latest vuejs version. The file is no longer used, instead using: <your_project_root>/vue.config.js.Grindelia
Your answer is correct, but please mention Vue version.Cantaloupe
do you mean the vue-cli version? vue version has no effect on thisMarchioness
The file myApp/config/index.js does not exist!Psychiatrist
Vue CLI 3 uses vue.config.js at project root. It has to be created manually IIRC.Daughterinlaw
there is no vue.config.js thereEndanger
sorry, I think with the latest update it has changed, you can update the answer one you figure it outMarchioness
"scripts": { "serve": "vue-cli-service serve --port 80" },Allix
Since this is the (un)helpful first listed response you also want to check: cli.vuejs.org/config/#vue-config-js under some sub section it is shown.Heaney
Also, you can use npm run serve -- --port 8085Saprogenic
P
25

Another option if you're using vue cli 3 is to use a config file. Make a vue.config.js at the same level as your package.json and put a config like so:

module.exports = {
  devServer: {
    port: 3000
  }
}

Configuring it with the script:

npm run serve --port 3000

works great but if you have more config options I like doing it in a config file. You can find more info in the docs.

Pugilism answered 19/8, 2018 at 0:37 Comment(1)
I like this answer as it shows that vue.config.js can be used to just change the port and leave everything else as-is which is what was asked originally.Wace
C
25

First Option:

OPEN package.json and add "--port port-no" in "serve" section.

Just like below, I have done it.

{
  "name": "app-name",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve --port 8090",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
}

Second Option: If You want through command prompt

npm run serve --port 8090

Cafeteria answered 29/10, 2020 at 7:4 Comment(0)
A
24

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"
},
Anybody answered 12/10, 2018 at 11:53 Comment(0)
C
17

If you want to change the localhost port, you can change scripts tag in package.json:

 "scripts": {
    "serve": "vue-cli-service serve --port 3000",
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  },
Carpel answered 17/3, 2020 at 18:2 Comment(0)
E
10

In the webpack.config.js:

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

You can change the port in the module.exports -> devServer -> port.

Then you restrat the npm run dev. You can get that.

Ethanol answered 3/2, 2018 at 12:13 Comment(0)
G
9

Oh my God! It is not that much complicated, with these answers which also works. However, other answers tho this question also works well.

If you really want to use the vue-cli-service and if you want to have the port setting in your package.json file, which your 'vue create <app-name>' command basically creates, you can use the following configuration: --port 3000. So the whole configuration of your script would be like this:

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

I am using @vue/cli 4.3.1 (vue --version) on a macOS device.

I have also added the vue-cli-service reference: https://cli.vuejs.org/guide/cli-service.html

Gyro answered 3/7, 2020 at 13:57 Comment(1)
This answer duplicates one on this page from 3 years ago: https://mcmap.net/q/118235/-how-to-change-port-number-in-vue-cli-projectPraline
J
8

If you use yarn:

yarn serve --port 3000
Jonathanjonathon answered 22/8, 2021 at 4:38 Comment(0)
L
8
  1. open package.json
  2. add script named serve, "serve": "Vue-cli-service serve --port 8081"
  3. npm run serve you will server run 8081
{
  "name": "app-name",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "serve": "vue-cli-service serve --port 8081", 
    "build": "vue-cli-service build",
    "lint": "vue-cli-service lint"
  }
}
Loathsome answered 17/7, 2022 at 1:39 Comment(0)
S
7

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.

Sigil answered 3/9, 2018 at 15:23 Comment(0)
L
7

There are a lot of answers here varying by version, so I thought I'd confirm and expound upon Julien Le Coupanec's answer above from October 2018 when using the Vue CLI. In the most recent version of Vue.js as of this post - [email protected] - the outlined steps below made the most sense to me after looking through some of the myriad answers in this post. The Vue.js documentation references pieces of this puzzle, but isn't quite as explicit.

  1. Open the package.json file in the root directory of the Vue.js project.
  2. Search for "port" in the package.json file.
  3. Upon finding the following reference to "port", edit the serve script element to reflect the desired port, using the same syntax as shown below:

    "scripts": {
      "serve": "vue-cli-service serve --port 8000",
      "build": "vue-cli-service build",
      "lint": "vue-cli-service lint"
    }
    
  4. Make sure to re-start the npm server to avoid unnecessary insanity.

The documentation shows that one can effectively get the same result by adding --port 8080 to the end of the npm run serve command like so: npm run serve --port 8080. I preferred editing the package.json directly to avoid extra typing, but editing npm run serve --port 1234 inline may come in handy for some.

Lecky answered 7/12, 2019 at 9:23 Comment(0)
A
5

To change the port (NPM), go to package.json. In scripts write your own script, for example:

"start": "npm run serve --port [PORT YOU WANT]"

After that you can start with npm start

"scripts":{"start":"npm run serve --- --port 3000"}

Abercrombie answered 22/7, 2021 at 8:56 Comment(1)
Please provide a detailed explanation to your answer, in order for the next user to understand your answer better. Also, provide a basic coverage of the content of your link, in case it stops working in the future.Schizophrenia
K
3

Add the PORT envvariable to your serve script in package.json:

"serve": "PORT=4767 vue-cli-service serve",
Kadiyevka answered 1/9, 2019 at 16:43 Comment(0)
C
3

If you want to change the port number temporarily, you can add a –port option to npm run serve command.

npm run serve -- --port 6058

Cordie answered 7/3, 2022 at 8:44 Comment(0)
T
2

You should be good with this:

"serve": "vue-cli-service serve --port 8081",

Tedra answered 20/7, 2022 at 1:30 Comment(0)
P
1

In my vue project in visual studio code, I had to set this in /config/index.js. Change it in the:

module.exports = {
    dev: {
          // Paths
          assetsSubDirectory: 'static',
          assetsPublicPath: '/',
          proxyTable: {},

          host: 'localhost', // can be overwritten by process.env.HOST
          port: 8090, // can be overwritten by process.env.PORT, if port is in use, a free one will be determined
          autoOpenBrowser: false,
          errorOverlay: true,
          notifyOnErrors: true,
          poll: false    
         }
}
Pupiparous answered 14/4, 2019 at 19:35 Comment(1)
I almost did the same. My file's name is 'vue.config.js' and is located at the root of the vue project. The demanded port is found on path module.exports.dev.port. Here it is set to 8090. The other key-value-pairs are not necessary (for me). That's for starting with 'npm run serve' on development mode!Heaney
I
0

If you are running this via Visual Studio Community or Professional (maybe with a .Net Core project) you will find that no matter what steps you do, when you launch the solution that it uses 8080.

Well there is launch.json file you need to edit hidden in the .vscode directory. MS don't tell you about this at all and a file search does not seem to find it.

Infralapsarian answered 23/8, 2022 at 15:32 Comment(0)
D
-2

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 => {}
}
Drachm answered 21/1, 2020 at 8:41 Comment(2)
go to node_modules/@vue/cli-service ... ? Doesn't that get overwritten on npm update?Fugato
you don't want to make code edits - should be configurableWilkey

© 2022 - 2024 — McMap. All rights reserved.