Next.js CLI

Next.js CLI 帮你启动、构建和导出(export)应用程序。

要列出所有可用的 CLI 命令,请在项目目录中运行以下命令:

npx next -h

(npx 随 npm 5.2+ 或更高版本一同分发)

输出如下所示:

Usage
  $ next <command>

Available commands
  build, start, export, dev, telemetry

Options
  --version, -v   Version number
  --help, -h      Displays this message

For more information run a command with the --help flag
  $ next build --help

你可以将任何 node 参数 传递给 next 命令:

NODE_OPTIONS='--throw-deprecation' next
NODE_OPTIONS='-r esm' next
NODE_OPTIONS='--inspect' next

构建

next build 将为你的应用程序构建一个creates an optimized production build of your application. The output displays information about each route.

  • Size – The number of assets downloaded when navigating to the page client-side. The size for each route only includes its dependencies.
  • First Load JS – The number of assets downloaded when visiting the page from the server. The amount of JS shared by all is shown as a separate metric.

The first load is colored green, yellow, or red. Aim for green for performant applications.

你可以在执行 next build 时使用 --profile 标记来开启 React 的with theflag in. This requires Next.js 9.5:

next build --profile

之后,你可以 you can use the profiler in the same way as you would in development.

You can enable more verbose build output with the --debug flag in next build. This requires Next.js 9.5.3:

next build --debug

With this flag enabled additional build output like rewrites, redirects, and headers will be shown.

Development 模式

next dev 在 development 模式下启动应用程序,并且starts the application in development mode with hot-code reloading, error reporting, and more:

默认情况下,该应用程序可以通过 http://localhost:3000 地址访问。通过 -p 参数可以修改默认端口号,例如:

npx next dev -p 4000

Production 模式

next start 也在 production 模式下启动应用程序。该应用程序应该首先通过 next build 命令进行编译。

默认情况下,该应用程序可以通过 http://localhost:3000 地址访问。通过 -p 参数可以修改默认端口号,例如:

npx next start -p 4000

遥测

Next.js 收集有关常规使用下的 完全匿名 的遥测数据。 此匿名计划并非强制参与,如果你不想共享任何信息,则可以选择退出。

有关遥测的更多信息,请阅读此文档