π Vue Build & Deployment β From Dev Server to Production (2025 Guide)
π§² Introduction β Why Learn Vue Build & Deployment?
When developing with Vue.js, running npm run serve
works great for local development. But before going live, your application must be compiled, optimized, and deployed. Vue CLI handles the build process efficiently, and you can deploy the output to platforms like GitHub Pages, Netlify, Firebase Hosting, Vercel, or a traditional web server.
π― In this guide, youβll learn:
- How to build your Vue app for production
- What the build command does behind the scenes
- Directory structure of the final build
- How to deploy to popular platforms
ποΈ 1. Building the Vue App for Production
After development:
npm run build
π¦ Output:
This generates a /dist
folder with minified and production-ready files:
dist/
βββ css/
βββ js/
βββ index.html
βββ favicon.ico
π§ vue.config.js
can customize this output if needed (e.g., outputDir
).
π§° 2. What Happens During Build?
Process Stage | Description |
---|---|
Code Minification | Removes whitespace, comments, unused code |
CSS Extraction | Moves inline styles to separate files |
Chunk Splitting | Breaks app into smaller, loadable pieces |
Environment Injection | Injects NODE_ENV=production vars |
βοΈ 3. vue.config.js
Build Configuration
Customize output directory, base URL, source maps, etc.
// vue.config.js
module.exports = {
publicPath: '/my-app/',
outputDir: 'dist',
assetsDir: 'static',
productionSourceMap: false
};
π€ 4. Deployment Methods
β A. GitHub Pages (Static Hosting)
npm install --save-dev gh-pages
Add to package.json
:
"scripts": {
"deploy": "gh-pages -d dist"
}
Then run:
npm run build
npm run deploy
π Set publicPath: '/your-repo-name/'
in vue.config.js
.
β B. Netlify
- Go to netlify.com
- Connect your GitHub repo
- Set build command:
npm run build
- Set publish directory:
dist/
β C. Firebase Hosting
npm install -g firebase-tools
firebase login
firebase init
Set dist
as public directory during setup.
firebase deploy
β D. Vercel
- Install Vercel CLI
- Run
vercel
in root directory - Choose
dist
as the output directory
β E. Custom Hosting (FTP or cPanel)
- Run
npm run build
- Upload files from
/dist/
to your serverβspublic_html
or root directory
π 5. Automating with CI/CD
Integrate Vue build into GitHub Actions, GitLab CI, or Jenkins for automatic deployment:
GitHub Actions Example:
name: Build & Deploy
on:
push:
branches: [main]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- run: npm install
- run: npm run build
- run: npm run deploy
π 6. Environment Variables
Place environment-specific settings in .env
files:
.env.production
.env.development
VUE_APP_API_URL=https://api.myapp.com
π§ Prefix with VUE_APP_
to expose in Vue.
π Summary β Recap & Next Steps
Vue’s build process optimizes your app for performance and security. Whether deploying to Netlify or GitHub Pages, understanding the build output and hosting options ensures a smooth go-live experience.
π Key Takeaways:
- Run
npm run build
to generate thedist/
folder - Use
vue.config.js
for custom paths or build tweaks - Deploy to Netlify, Firebase, GitHub Pages, or your own server
- Manage environments with
.env
files
βοΈ Real-World Relevance:
This workflow is essential for publishing SPAs, PWAs, admin dashboards, landing pages, and production-ready frontend systems.
β FAQ Section
β What does npm run build
do in Vue?
β
It creates a minified, optimized production version of your app in the /dist
folder.
β Where should I deploy my Vue app?
β Use Netlify, Firebase Hosting, GitHub Pages, Vercel, or traditional web hosting.
β Can I change the output folder of the Vue build?
β
Yes. Set outputDir
in vue.config.js
.
β How do I set a custom base path for deployment?
β
Set publicPath: '/custom-path/'
in vue.config.js
.
β How do I use environment variables in Vue?
β
Define them in .env.production
using the VUE_APP_
prefix.
Share Now :