Commands and Deployment

Nuxt.js comes with a set of useful commands, both for development and production purpose.

List of Commands

You can now run different commands depending on the target

server

CommandDescription
nuxt devLaunch a development server on localhost:3000 with hot-reloading.
nuxt buildBuild your application with webpack and minify the JS & CSS (for production).
nuxt startStart the server in production mode (after running nuxt build).

static

CommandDescription
nuxt devLaunch a development server on localhost:3000 with hot-reloading.
nuxt startServe your production application from dist/ directory (Nuxt >= v2.13).
nuxt generateBuild the application and generate every route as a HTML file (used for static hosting).

Arguments

You can use --help with any command to get detailed usage. Common arguments are:

  • --config-file or -c: specify the path to nuxt.config.js file.
  • --spa or -s: Runs command in SPA mode and disables server side rendering.
  • --unix-socket or -n: specify the path to a UNIX socket.

Hooks

HookObjective
cli:buildErrorCaptures build errors in dev mode and display them on loading screen

Using in package.json

You should put these commands in the package.json:

  1. "scripts": {
  2. "dev": "nuxt",
  3. "build": "nuxt build",
  4. "start": "nuxt start",
  5. "generate": "nuxt generate"
  6. }

Then, you can launch your commands via npm run <command> (example: npm run dev).

Pro tip: to pass arguments to npm commands, you need an extra -- script name (example: npm run dev -- --spa).

Development Environment

To launch Nuxt in development mode with hot reloading:

  1. nuxt
  2. // OR
  3. npm run dev

Production Deployment

Nuxt.js lets you choose between three modes to deploy your application: SSR, Static Generated, or SPA.

Server-Side Rendered Deployment (Universal SSR)

To deploy, instead of running nuxt, you probably want to build ahead of time. Therefore, building and starting are separate commands:

  1. nuxt build
  2. nuxt start

You can also set server.https in your nuxt.config.js with the same set of options passed to https.createServer, should you choose to serve Nuxt.js in HTTPS mode. Unix sockets are also available if you set the server.socket option in nuxt.config.js (or -n in the CLI). When using Unix sockets, make sure not to set the host and port parameters otherwise the socket parameter is ignored.

The package.json like follows is recommended:

  1. {
  2. "name": "my-app",
  3. "dependencies": {
  4. "nuxt": "latest"
  5. },
  6. "scripts": {
  7. "dev": "nuxt",
  8. "build": "nuxt build",
  9. "start": "nuxt start"
  10. }
  11. }

Note: we recommend putting .nuxt in .npmignore or .gitignore.

Static Generated Deployment (Pre-rendered)

Nuxt.js gives you the ability to host your web application on any static hosting.

To generate our web application into static files:

  1. "scripts": {
  2. "generate": "nuxt generate"
  3. }

In your nuxt.config file you need to add the target property with the value of static nuxt.config.js. (For Nuxt >= 2.13:)

  1. export default {
  2. target: 'static'
  3. }
  1. npm run generate

Nuxt.js will create a dist folder with everything inside ready to be deployed on a static hosting service.

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.

  1. npm run generate --fail-on-error
  2. // OR
  3. yarn generate --fail-on-error

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.

Warning: dynamic routes are ignored by the generate command when using Nuxt <= v2.12: API Configuration generate

When generating your web application with nuxt generate, the context given to asyncData and fetch will not have req and res.

Single Page Application Deployment (SPA)

nuxt generate still needs its SSR engine during build/generate time while having the advantage of having all our pages pre rendered, and have a high SEO and page load score. The content is generated at build time. For example, we can’t use it for applications where content depends on user authentication or a real time API (at least for the first load).

The SPA idea is simple! When SPA mode is enabled using mode: 'spa' or --spa flag, and we run build, generation automatically starts after the build. This generation contains common meta and resource links, but not page content.

So, for an SPA deployment, you must do the following:

  • Change mode in nuxt.config.js to spa.
  • Run npm run build.
  • Deploy the created dist/ folder to your static hosting like Surge, GitHub Pages or nginx.

Another possible deployment method is to use Nuxt as a middleware in frameworks while in spa mode. This helps reduce server load and uses Nuxt in projects where SSR is not possible.

Read our FAQ and find nifty examples for deployments to popular hosts.