Commands and Deployment
Using in package.json
You should have these commands in your package.json:
"scripts": {
"dev": "nuxt",
"build": "nuxt build",
"start": "nuxt start",
"generate": "nuxt generate"
}You can launch your commands via yarn <command> or npm run <command> (example: yarn dev / npm run dev).
Development Environment
To launch Nuxt in development mode with hot module replacement on http://localhost:3000:
yarn devnpm run devList of Commands
You can run different commands depending on the target:
target: server (default value)
- nuxt dev - Launch the development server.
- nuxt build - Build and optimize your application with webpack for production.
- nuxt start - Start the production server (after running
nuxt build). Use it for Node.js hosting like Heroku, Digital Ocean, etc.
target: static
- nuxt dev - Launch the development server.
- nuxt generate - Build the application (if needed), generate every route as a HTML file and statically export to
dist/directory (used for static hosting). - nuxt start - serve the
dist/directory like your static hosting would do (Netlify, Vercel, Surge, etc), great for testing before deploying.
Webpack Config Inspection
You can inspect the webpack config used by Nuxt to build the project (similar to vue inspect).
- nuxt webpack query...
Arguments:
--name: Bundle name to inspect. (client, server, modern)--dev: Inspect webpack config for dev mode--depth: Inspection depth. Defaults to 2 to prevent verbose output.--no-colors: Disable ANSI colors (disabled by default when TTY is not available or when piping to a file)
Examples:
nuxt webpacknuxt webpack devtoolnuxt webpack resolve aliasnuxt webpack module rulesnuxt webpack module rules test=.jsxnuxt webpack module rules test=.pug oneOf use.0=rawnuxt webpack plugins constructor.name=WebpackBar options reporternuxt webpack module rules loader=vue-nuxt webpack module rules "loader=.*-loader"
Production Deployment
Nuxt lets you choose between Server or Static deployments.
Server Deployment
To deploy a SSR application we use target: 'server', where server is the default value.
yarn buildnpm run buildNuxt will create a .nuxt directory with everything inside ready to be deployed on your server hosting.
.nuxt in .npmignore or .gitignore.Once your application is built you can use the start command to see a production version of your application.
yarn startnpm run startStatic Deployment (Pre-rendered)
Nuxt gives you the ability to host your web application on any static hosting.
To deploy a static generated site make sure you have target: 'static' in your nuxt.config.js (for Nuxt >= 2.13):
export default {
target: 'static'
}yarn generatenpm run generateNuxt will create a dist/ directory with everything inside ready to be deployed on a static hosting service.
dist/ in .npmignore or .gitignore.As of Nuxt v2.13 there is a crawler installed that will now crawl your link tags and generate your routes when using the command nuxt generate based on those links.
generate command when using Nuxt <= v2.12: API Configuration generatenuxt generate, the context given to asyncData and fetch will not have req and res.Fail on Error
To return a non-zero status code when a page error is encountered and let the CI/CD fail the deployment or build, you can use the --fail-on-error argument.
yarn generate --fail-on-errornpm run generate --fail-on-errorWhat's next?
Was this helpful?