Looking for: http://localhost:5173/?
lsof -i :5173
(Mac/Linux)netstat -ano | findstr :5173
(Windows)
npm run dev -- --port 5174
vite --port 5174
npm run dev -- --host
vite --host 0.0.0.0
npm run build
npm run preview
(preview build)
Essential vite.config.js configurations for development:
// vite.config.js
export default defineConfig({
server: {
port: 5173,
open: true,
cors: true
}
})
server: {
proxy: {
'/api': {
target: 'http://localhost:3000',
changeOrigin: true
}
}
}
Running Vite applications in Docker containers:
FROM node:18-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 5173
CMD ["npm", "run", "dev", "--", "--host"]
FROM node:18-alpine as build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM nginx:alpine
COPY --from=build /app/dist /usr/share/nginx/html
Port 5173 is commonly used by:
This error occurs when another application is already using port 5173. Solutions:
npm run dev -- --port 5174
export default defineConfig({ server: { port: 5174 } })
netstat -ano | findstr :5173
then taskkill /PID [PID] /F
lsof -i :5173
then kill -9 [PID]
If changes aren't automatically reflecting in the browser:
By default, Vite's development server only listens on localhost:
npm run dev -- --host
export default defineConfig({ server: { host: '0.0.0.0' } })
http://YOUR_IP:5173
When Vite can't find your modules or imports fail:
rm -rf node_modules && npm install
When production builds fail or take too long:
npm run type-check
npm run build -- --analyze
npm install @vitejs/plugin-vue
Enable Vue SFC support and HMR
npm install @vitejs/plugin-react
Fast refresh and JSX support
npm install typescript @types/node
Built-in TypeScript compilation
// .env files
VITE_API_URL=http://localhost:3000
VITE_APP_TITLE=My App
Access with import.meta.env.VITE_*
const Component = lazy(() => import('./Component'))
vite-bundle-analyzer
to visualize bundle sizenpm install sass less stylus
Built-in support, just import .scss/.less files
// postcss.config.js
export default {
plugins: [require('autoprefixer')]
}
css: {
preprocessorOptions: {
stylus: {
additionalData: '@import "./src/vars.styl"'
}
}
}
resolve: {
alias: {
'@': path.resolve(__dirname, './src'),
'~': path.resolve(__dirname, './src')
}
}
node --version
npm list vite
rm -rf node_modules/.vite
server: { https: true }
for testing HTTPSnpm audit
regularly--mode production
for production buildsbuild: { sourcemap: false }