Change default port number in Vue CLI App
Short article on how to change the default port number i.e 8080 in vue-cli via terminal and package.json file.
In this short tutorial, we will learn how to change the default port number of Vue CLI Application.
When we run a Vue-CLI project using npm run serve , it runs on its default port number i.e 8080. And if the default port is busy then the port number is increased by +1 and it runs on the next port 8081 and so on.
Let's see how to change the default port and set our own when we run our Vue application.
Change Default Port via Terminal
In Vue CLI 3.0, we can change the port just by adding the port number after the --port option. We have to append --port with the port number after npm run serve command.
npm run serve -- --port 3000
The first -- is used to pass the port option to the npm script and not to the npm itself.
This is a temporary way to change the default port number of the Vue application.
Change Port Number in package.json file
Now, if you don't want to type the port number every time when running the Vue app, then you can just do the following change in your package.json file.
"scripts": { "serve": "vue-cli-service serve --host localhost --port 3000", "build": "vue-cli-service build" },
Just add the --port 3000 at the end of the serve property.
After the change, you can run the app using npm run serve and it will run the app on localhost:3000.
Change Port Number via vue.config.js
The vue.config.js is an optional config file that is loaded by @vue/cli-service automatically. Using this file to change our development server port number in Vue.
- First, create a file in the root directory of your project and name it vue.config.js.
- Now inside the file, add this code and change the port number of your choice. I have changed it to 4000.
module.exports = { devServer: { port: 4000 } }
Now when you run the app using npm run serve, you can see the given port number in your localhost. It's changed successfully.
DONE Compiled successfully in 5886ms 3:29:06 PM App running at: - Local: http://localhost:4000/ - Network: http://localhost:4000/ Note that the development build is not optimized. To create a production build, run npm run build.
Related Topics:
Related Posts
Easy Way to use localStorage with Vue
Short article on how to use local storage with Vue and learn saving and storing data in local storage with the setItem() and getItem() methods.
Force update Vue to Reload/Rerender component
Short tutorial on how to correctly force update Vue.js component to reload or rerender. The component is reinitialized without having to do a browser refresh.
Set and get cookies in a browser in Vue App
Find out how to set and get cookies in a browser for your webpage in Vue using vue-cookies package.
Save vuex state after page refresh in Vue App
Short tutorial on how to save (or persist) data in a vue application using Vuex and vuex-persist npm package.
