@ngrok/nextjs is a command that starts next dev and opens an ngrok endpoint to it as one process:
Ctrl+C tears down both the endpoint and next dev, including the worker processes next dev starts.
The command is built on the JavaScript SDK, so there’s no separate ngrok binary to install.
@ngrok/nextjs is published on npm at version 0.1.0 and maintained in the ngrok-samples/ngrok-nextjs repository.What you’ll need
- An ngrok account.
- Your ngrok auth token.
- Node.js 18 or later.
- A Next.js project.
1. Set your auth token
Add your auth token to.env.local in your Next.js project root:
.env.local
.env.local itself, so you don’t need to export the variable in your shell.
An NGROK_AUTHTOKEN already set in your shell takes precedence over the file.
2. Run your app
next dev on port 3000, opens an endpoint to it, and prints the public URL.
To run on a different port, set PORT:
3. Test your endpoint
Open the printed URL in your browser. You see your Next.js app, served through ngrok. The command also:- Writes the public URL into
.env.localasNEXTAUTH_URLandNEXT_PUBLIC_APP_URL, so auth libraries and client code pick it up automatically. The command leaves other lines in the file as they are. - Prints a QR code so you can open the URL on your phone.
Set
NGROK_QR=falseto turn it off. - Prints a reminder, the first time you run it in a project, to register the URL with your OAuth and webhook providers.
Configuration
Createngrok.config.ts (or .js or .mjs) in your project root to configure the endpoint:
ngrok.config.ts
Use your own domain
Seturl to a domain you’ve reserved on your account.
There are three places to set it, checked in this order:
- The
NGROK_DOMAINenvironment variable, in your shell or in.env.local. A bare domain getshttps://added for you. ngrok.config.local.ts, a personal config file with the same shape asngrok.config.ts. Add it to.gitignore.ngrok.config.ts, the config file you commit and share with your team.
ERR_NGROK_334.
Multiple endpoints
To front more than one local service from a single command, such as the Next.js app plus a co-located API server, list them underendpoints:
ngrok.config.ts
upstream: a port number, which forwards to http://localhost:<port>, or any address string the JavaScript SDK accepts, such as https://localhost:8443.
Entries under endpoints don’t write to .env.local unless you give them an env.url.
Every endpoint needs its own url, or pooling: true on each endpoint that shares one.
If two endpoints would share a URL without pooling, including two endpoints that both omit url and land on your dev domain, the command exits with an error before opening a session.
Without that check, ngrok lets the most recently opened listener win and the other goes unreachable with no error.
Limits and availability
- The command only parses
ngrok.config.tsandngrok.config.local.ts. It doesn’t read the agent config file. NGROK_DOMAINonly applies to the endpoint thatnext devruns on. Seturlon each entry inendpointsdirectly.- An
internalbinding requires a URL that ends in.internal. ngrok rejects any other URL withERR_NGROK_9029. - Endpoint counts and domains follow your plan’s limits. See free plan limits.
What’s next?
- Browse Traffic Policy examples to add authentication, restrict access by IP, or rewrite URLs.
- Read the JavaScript SDK quickstart to see what the command does under the hood.
- See the other framework libraries for Gin, FastAPI, and Axum.
- Report issues or read the source in the ngrok-samples/ngrok-nextjs repository.