Next.js - CLI build Command



In Next.js CLI, the `build` command is used to create a production-ready build of a Next.js application. This command provides various options to customize the build process and environment. In this chapter, we will explain how to use the `build` command to create a customizable Next.js production build.

Next.js Build Command Syntax

Following is the syntax of the build command in Next.js CLI.

npx next build [options]

For example, npx next build --no-lint. Here, the production build will occur on current directory, with linting disabled.

Options of Build Command

Following is a list of options available for the `build` command.

Options Explanation
-h, --help Show all available options.
[directory] Specifies a directory to build the application. If not provided, the current directory is used.
-d or --debug Enables a more verbose build output. Includes additional build details like rewrites, redirects, and headers.
--profile Enables production profiling for React.
--no-lint Disables linting (error suggestion) during the build process.
--no-mangling Disables mangling, which might affect performance and is intended for debugging purposes.
--experimental-app-only Builds only App Router routes.
--experimental-build-mode [mode] Uses an experimental build mode. Choices include "compile", "generate", or the default "default".

Specify Directory For Building App

If your current terminal directory does not contain Next.js project, then you can use `build` command with directory option to build app in a different directory. If you don't specify any directory, by default current directory is used for building app. Following is the syntax to specify the directory.

npx next build ./next-js-example

After running the above command in your terminal, the Next.js build process will create a production-ready version of the application located in the `./next-js-example` directory.

Output

In the output, app build occurs at specified directory.

Build Directory

Avoid linting on Building App

In Next.js build process, linting refers to the automatic checking of your code for potential issues, such as errors, style violations, or patterns that may lead to bugs. You can avoid linting using following command.

npx next build ./next-js-example --no-lint

Output

In the output, you can see the build process for the `./next-js-example` directory with linting disabled.

next.js-build-command-no-linting-output
Advertisements