Running a Vue app lets you see your work in a browser while building prepares your app for sharing with others.
Running and building a Vue app
npm run dev npm run build npm run preview
npm run dev starts a local server to run your app during development.
npm run build creates optimized files for production use.
npm run preview starts a local server to preview the built app.
npm run dev
npm run build
npm run preview
This example shows the commands you type to run and build your Vue app step-by-step.
/* Steps to run and build a Vue app */ // 1. Open terminal in your Vue project folder // 2. Run the development server: npm run dev // 3. After finishing development, build the app: npm run build // 4. Preview the built app locally: npm run preview
Make sure you have Node.js and npm installed before running these commands.
Use a modern browser to open the local URL shown after running npm run dev.
The dist folder contains your built app ready to upload to a web server.
Use npm run dev to run your Vue app locally during development.
Use npm run build to create optimized files for production.
Use npm run preview to check the built app before sharing.
Practice
Solution
Step 1: Understand the purpose of each command
npm run devis used to run the app locally for development,npm run buildcreates production files, andnpm run previewpreviews the production build.Step 2: Match the command to running locally
Since running locally during development means seeing live changes,npm run devis the correct command.Final Answer:
npm run dev -> Option CQuick Check:
Run locally = npm run dev [OK]
- Confusing build with dev command
- Using npm start instead of npm run dev
- Trying to preview before building
Solution
Step 1: Identify the command for production build
npm run buildis the standard command to create optimized files for production in Vue apps.Step 2: Eliminate other options
npm run devruns the app locally,npm run previewpreviews the build, andnpm run startis not a default Vue command.Final Answer:
npm run build -> Option BQuick Check:
Build production = npm run build [OK]
- Using npm run dev to build
- Typing npm run start which is not default
- Confusing preview with build
npm run preview after building your Vue app?Solution
Step 1: Understand the purpose of npm run preview
This command serves the production build locally so you can check how the optimized app works before sharing.Step 2: Compare with other options
It does not rebuild or delete files, nor does it run development mode. It only previews the built app.Final Answer:
It starts a local server to preview the production build -> Option AQuick Check:
Preview = local server for production build [OK]
- Thinking preview rebuilds the app
- Confusing preview with dev mode
- Assuming preview deletes files
npm run build but your dist folder is empty. What is the most likely cause?Solution
Step 1: Check if build script exists
Ifnpm run buildruns but produces no files, likely the build script is missing or misconfigured inpackage.json.Step 2: Rule out other causes
Missing dependencies cause errors, not empty folders. Running dev or preview does not create build files. So missing build script is the main cause.Final Answer:
Your package.json has no build script defined -> Option DQuick Check:
Empty build folder = missing build script [OK]
- Assuming npm install fixes empty dist
- Confusing dev and build commands
- Trying preview before building
Solution
Step 1: Build the production files first
You must runnpm run buildto create optimized files for production.Step 2: Preview the production build locally
After building, runnpm run previewto start a local server and check the production version before sharing.Final Answer:
npm run build, then npm run preview -> Option AQuick Check:
Build then preview to check production [OK]
- Previewing before building
- Running dev instead of preview for production check
- Running preview then build in wrong order
Ready to prove it?
Pass the test and this pattern moves unknown
