This article was originally posted on my personal blog.
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
toserver.ts
.Update the
scripts
inpackage.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"
}
}
}
Top comments (7)
It doesn't work for me. I'm getting this error:
\node_modules\ts-node\src\index.ts:859
return new TSError(diagnosticText, diagnosticCodes, diagnostics);
^
TSError: β¨― Unable to compile TypeScript:
error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.
Modify "bundler" to "node" in the ts.config.json file. You can view this tempalte.
github.com/vercel/next.js/blob/c30...
Same issue as @inaara_kalani
Got this error
`tsconfig.server.json:3:5 - error TS5095: Option 'bundler' can only be used when 'module' is set to 'preserve' or to 'es2015' or later.
3 "compilerOptions": {
~~~~~~~~~~~~~~~~~
Found 1 error in tsconfig.server.json:3`
Modify "bundler" to "node" in the ts.config.json file. You can view this tempalte.
github.com/vercel/next.js/blob/c30...
And? Wtf? Is that all?
Loool. Really worked. Thanks!
Awesome π₯