What are the steps to successfully deploy a static website created with Next.js on Vercel?

Using the Next.js static site generator, I created a simple static site that I now want to deploy on Vercel. However, I keep encountering an error during the build process. While I have successfully deployed this site on other static hosting platforms before, I am eager to give Vercel a try.

The nextjs documentation states that my app requires zero configuration:

We strongly recommend using Vercel even if your Next.js app is fully static. Vercel is optimized to make static Next.js apps blazingly fast. next export works with Zero Config deployments on Vercel.

Below you can find my package.json file and the deployment scripts that run npm export, which triggers
next build && next export
to generate the out/ directory as per the docs:

{
  "name": "new-barber-shop",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "dev": "next",
    "build": "next build && next export",
    "export": "next export",
    "start": "next start"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@zeit/next-css": "^1.0.1",
    "google-maps-react": "^2.0.2",
    "next": "^9.1.2",
    "react": "^16.11.0",
    "react-bootstrap": "^1.0.0-beta.14",
    "react-dom": "^16.11.0",
    "semantic-ui-react": "^0.88.1"
  }
}

Here is a snippet from the Vercel deployment build logs showing the encountered error:

22:42:26.610
Compiled successfully.
...
Error: Cannot export when target is not server. https://err.sh/zeit/next.js/next-export-serverless
22:42:27.445
    at _default (/vercel/path0/node_modules/next/dist/export/index.js:1:2956)
...

Although the compilation was successful, it seems to fail at the export stage. Any suggestions on how to tackle this issue would be greatly appreciated.

Additionally, here is my next.config.js configuration:

module.exports = {
  target: 'serverless',
    exportPathMap: function() {
      return {
        '/': { page: '/' }
      };
    }
  };

Error log generated when executing npm run build locally in the project (mirroring the error output experienced during deployment on Vercel):

$ npm run build

> [email protected] build F:\Austin\web-apps\new-barber-shop
> next build && next export

Browserslist: caniuse-lite is outdated. Please run next command `npm update`
Creating an optimized production build  

Compiled successfully.

Automatically optimizing pages  
...

Error: Cannot export when target is not server. https://err.sh/zeit/next.js/next-export-serverless
...

Answer №1

If you're encountering issues, it may be caused by having

"target": "serverless"
in your next.config.js. Try removing it or changing it to
"target": "server"
.

Next.js requires the target to be set to server for proper exporting (which is the default setting). A serverless build lacks a request handler, typically provided by hosting services.


UPDATE: The error could stem from using an outdated version of Next.js, likely because Zeit (now Vercel) was misunderstood as a serverless platform leading to target overrides. Updating the version should resolve the issue. Additionally, remember to include .next, out, etc. in your .gitignore file. Check out the updated repository here.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Safari experiencing sporadic issues with reCAPTCHA AJAX API within modal dialogs

I am currently utilizing the reCAPTCHA AJAX API to showcase the captcha in a modal dialog box. To display the boxes, I am incorporating jqModal, and opting for the AJAX version of reCAPTCHA due to compatibility issues with the PHP version when used with jq ...

No longer will popups be blocked

I've been trying to work with jQuery to create a popup. But I'm facing an issue where my code is being blocked by Google. Here is the code snippet: <script type="text/javascript"> $(document).ready(function () { var flag = true; ...

What is the best way to enhance a state's capabilities in Machina.js?

When using Machina.js (version 0.3.6), how can I instantiate a modified FSM constructor where both the child and parent FSMs define behaviors in the same states? Here is the code snippet: var _ = require('lodash'); var machina = require('m ...

Managing and displaying information provided through forms

I'm currently developing a URL shortening tool, but I'm encountering difficulties in extracting jQuery form values to generate the shortened URL text. You can view the form layout here: <form name="urlForm"> <input type="text" name ...

What Happens When You Click on a Link in Google News Using the Mobile Chrome Browser

When using the Chrome Browser on mobile to open a news link from Google News, a new tab is opened. To return to Google News, users can click on the white-highlighted "left arrow" below or find a small header with a "<" symbol above to navigate back. Ho ...

What is the better choice in NodeJS: using "return cb(..)" or first calling cb(..) and then returning?

Forgive me if this sounds like a silly question, but I'm curious about the implications of something: Whenever I encounter an error or need to complete a function flow, I follow certain instructions such as: if(err) { cb(err); // or for exampl ...

There seems to be an issue with node.js - headers cannot be set after they have already been sent to

As I work on developing my blog, I've encountered an issue with rendering different paths using the router parameter. Each time I attempt to display another route, an error surfaces. Unfortunately, I'm unable to provide more details at this momen ...

Spotting the Visible Element; Detecting the Element in

Currently, my webpage has a fixed header and I am facing the challenge of dynamically changing the styling of the li elements within the navigation based on the user's scrolling position. To achieve this, I believe the most effective approach would b ...

Creating a looping animation on multiple elements using jQuery that makes them fade in and out at different intervals

I am currently working on creating a fade in and out animation for multiple elements using jquery. It's a bit complex to explain, so I will start by sharing the relevant code. $(document).ready(function() { $("#1").delay(0).animate({ ...

Tips for simulating a service in Angular unit tests?

My current service subscription is making a promise: getTaskData = async() { line 1 let response = this.getTaskSV.getTaskData().toPromise(); line 2 this.loading = false; } I attempted the following approach: it('should load getTaskData', ...

Next.js Image staying at the forefront

Currently, I am utilizing Tailwind CSS to style my Next.js website and incorporating the Next.js Image element for an image on the page. My goal is to display a modal over the existing page without completely blacking out the background; instead, I want i ...

Show HTML form elements on the page using jQuery or JavaScript

Is there a jQuery or JS library that can perform the following task: If the user inputs 'x' in A01, then make A01_1 visible. Otherwise, do nothing. <form id="survey"> <fieldset> <label for="A01">A01</label&g ...

The curious case of unusual behavior in jQuery's livequery() method

When attempting to use the jQuery plugin "livequery" to highlight certain words within dynamically generated search results, the highlighting does not appear to work! Strangely, adding an alert() function before executing the code causes the highlighting ...

What techniques can be used to avoid the MUI `DataGrid` from constantly re-rendering when a row is committed?

Check it out here to see how the MUI documentation implemented it: <b>const</b> rows = [/* Row Data */] <DataGrid rows={rows} {/* Other Props */} /> <sup>/*[1]*/</sup> The approach taken by MUI is quite impressive. It streaml ...

Is there a way to apply filters to jquery datatables during initialization process?

Is there a way to server-side filter datatable during initialization? I attempted the following code: function tableFilter(arg1, param2, value3) { var searchTable = $("#tblsearch").dataTable({ "bRetrieve": true, "b ...

Replacing text within a paragraph using D3.js

Is it possible to develop a D3 function that can choose a paragraph, delete its content, and replace it with new text? If so, what would be the most effective way to accomplish this? I attempted the following: d3.select("#triggerButton") .on("clic ...

How can I stop Jquery Mobile from processing the URL hash?

Currently implementing Jquery Mobile in a preexisting web application is posing a challenge. The issue arises as Jquery Mobile is interpreting all URL hashes by default. For example: mysite.com/#foo In this case, the hash 'foo' is being directe ...

Exploring the functions of `map` and `filter` in the world of

Consider this input: var m = [{ name: 'foo', routes: [{verb: 'post', path: '/foo1'}, {verb: 'get', path: '/foo2'}] }, { name: 'bar', routes: [{verb: 'put', path: ...

Setting up lint-staged for Vue projects: A step-by-step guide

After setting up a new Vue3 app using the Vue CLI and configuring Prettier as my linter, I decided to implement commitlint, husky, and lint-staged for validating commit messages and linting the code before pushing it. My Approach Following the instructio ...

Is it possible to access the current user session directly from the RootLayout?

Dealing with a basic AppBar using MUI, I'm trying to determine whether the user is logged in or not. The AppBar is located within the RootLayout, and it seems to be causing the issue. I'm unable to find a native method to fetch the current user ...