There is an official NextJS guide on how to create a custom server. However, it only works with JavaScript. Please follow their guide and come back.
Rename server.js
to server.ts
.
Update the scripts
in package.json
"scripts": {
"dev": "ts-node server.ts",
"build": "next build",
"start": "NODE_ENV=production ts-node server.ts"
}
Install ts-node
yarn add ts-node
Add this section in tsconfig.json
{
// ...
"ts-node": {
"transpileOnly": true,
"compilerOptions": {
"module": "commonjs"
}
}
}