rsnext/docs/api-reference/cli.md
Tim Neutkens cb84bc5daf
Remove deprecated features and enable future flag (#26066)
- Enables excludeDefaultMomentLocales by default
- Adds distDir cleaning (See RFC #6009)
- Adds support for `PORT`
- Removes `router.events` from the server-side router as it should not be used server-side (long-standing todo that is potentially breaking). Note that it's still available as `Router.events` (import Router from 'next/router') and with `useRouter` in `useEffect`. Using it with `useEffect` is the correct way and I've updated the upgrading guide to reflect that
- Added webpack 5 to the upgrading guide
- Removed `Head.rewind` as it's been a no-op since Next.js 9.5 and can now be safely removed from user code

Fixes #11408 
Fixes #10338
Fixes #5554



## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added

## Feature

- [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [x] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.

## Documentation / Examples

- [ ] Make sure the linting passes
2021-06-14 14:20:34 +00:00

127 lines
3.8 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
description: The Next.js CLI allows you to start, build, and export your application. Learn more about it here.
---
# Next.js CLI
The Next.js CLI allows you to start, build, and export your application.
To get a list of the available CLI commands, run the following command inside your project directory:
```bash
npx next -h
```
_([npx](https://medium.com/@maybekatz/introducing-npx-an-npm-package-runner-55f7d4bd282b) comes with npm 5.2+ and higher)_
The output should look like this:
```bash
Usage
$ next <command>
Available commands
build, start, export, dev, lint, telemetry
Options
--version, -v Version number
--help, -h Displays this message
For more information run a command with the --help flag
$ next build --help
```
You can pass any [node arguments](https://nodejs.org/api/cli.html#cli_node_options_options) to `next` commands:
```bash
NODE_OPTIONS='--throw-deprecation' next
NODE_OPTIONS='-r esm' next
NODE_OPTIONS='--inspect' next
```
## Build
`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.
You can enable production profiling for React with the `--profile` flag in `next build`. This requires [Next.js 9.5](https://nextjs.org/blog/next-9-5):
```bash
next build --profile
```
After that, 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:
```bash
next build --debug
```
With this flag enabled additional build output like rewrites, redirects, and headers will be shown.
## Development
`next dev` starts the application in development mode with hot-code reloading, error reporting, and more:
The application will start at `http://localhost:3000` by default. The default port can be changed with `-p`, like so:
```bash
npx next dev -p 4000
```
Or using the `PORT` environment variable:
```bash
PORT=4000 npx next dev
```
> Note: `PORT` can not be set in `.env` as booting up the HTTP server happens before any other code is initialized.
You can also set the hostname to be different from the default of `0.0.0.0`, this can be useful for making the application available for other devices on the network. The default hostname can be changed with `-H`, like so:
```bash
npx next dev -H 192.168.1.2
```
## Production
`next start` starts the application in production mode. The application should be compiled with [`next build`](#build) first.
The application will start at `http://localhost:3000` by default. The default port can be changed with `-p`, like so:
```bash
npx next start -p 4000
```
Or using the `PORT` environment variable:
```bash
PORT=4000 npx next start
```
> Note: `PORT` can not be set in `.env` as booting up the HTTP server happens before any other code is initialized.
## Lint
`next lint` runs ESLint for all files in the `pages`, `components`, and `lib` directories. It also
provides a guided setup to install any required dependencies if ESLint is not already configured in
your application.
If you have other directories that you would like to lint, you can specify them using the `--dir`
flag:
```bash
next lint --dir utils
```
## Telemetry
Next.js collects **completely anonymous** telemetry data about general usage.
Participation in this anonymous program is optional, and you may opt-out if you'd not like to share any information.
To learn more about Telemetry, [please read this document](https://nextjs.org/telemetry/).