To ensure proper functioning, the next.js project and custom server must be built separately.
- Begin by removing any existing
.next
and dist
folders
- In the root of the project, create a new tsconfig file named
tsconfig.server.json
that extends the primary tsconfig.json
configuration. This setup assumes the custom server resides in a directory called ./server
, and you desire the output to be stored in ./dist
.
{
"extends": "./tsconfig.json",
"compilerOptions": {
"module": "commonjs",
"outDir": "dist",
"target": "es2017",
"isolatedModules": false,
"noEmit": false
},
"include": ["server"]
}
- Update your scripts within the
package.json
file
"scripts": {
"build:server": "tsc --project tsconfig.server.json",
"build:next": "next build",
"build": "npm run build:next && npm run build:server",
"start:prod": "NODE_ENV=production node dist/server/index.js",
...
}