There are several ways to deploy your Next.js application, with Vercel offering the most streamlined experience as the creators of Next.js, or you can self-host your application.
Vercel provides a zero-configuration deployment experience for Next.js applications 12:
vercel
This command, run from your project's root directory, will deploy your application to Vercel. For production deployments, use:
vercel --prod
If you prefer to self-host, Next.js supports three main approaches 12:
Ensure your package.json
has the proper scripts:
{
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start"
}
}
Then build and start your application:
npm run build
npm run start
You can deploy Next.js using Docker:
docker build -t nextjs-docker .
docker run -p 3000:3000 nextjs-docker
For static sites or SPAs that don't require server features:
next build
next export
This generates static HTML files that can be deployed to any static hosting service.
next start
12next start
but not with static exports 12NEXT_PUBLIC_
to expose to the browser 12When using the Vercel CLI, you have additional options 3:
--prebuilt
: Deploy a project that was built locally--build-env
: Provide environment variables for the build step--env
: Provide runtime environment variables--regions
: Specify which regions to deploy toLet me guide you through the process of setting up a Next.js application and deploying it to Vercel. This is a straightforward process that will get your application online quickly.
If you don't already have a Next.js application, you can create one using the following command:
npx create-next-app@latest my-next-app
cd my-next-app
This will set up a new Next.js project with the latest features, including the App Router.
Vercel works best with Git-based deployments. If your project isn't already in a Git repository:
git init
git add .
git commit -m "Initial commit"
Create a repository on GitHub, GitLab, or Bitbucket and push your code:
git remote add origin https://github.com/yourusername/my-next-app.git
git branch -M main
git push -u origin main
There are two ways to deploy to Vercel:
Configure your project settings:
Install the Vercel CLI:
npm install -g vercel
Log in to Vercel:
vercel login
Deploy from your project directory:
vercel
For production deployments:
vercel --prod
If your application uses environment variables, you can add them in the Vercel dashboard:
For local development, create a .env.local
file in your project root:
DATABASE_URL=your_database_url
API_KEY=your_api_key
Remember to add .env.local
to your .gitignore
file to keep sensitive information out of your repository.
Once set up, Vercel will automatically deploy:
After deployment, Vercel will provide you with a URL where your application is live (e.g., https://my-next-app.vercel.app
). You can visit this URL to see your deployed application.
Now that your application is deployed, you might want to: