DEV Community

Andrei Canta
Andrei Canta

Posted on • Originally published at deiucanta.com

Custom NextJS Server with Typescript

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.

https://nextjs.org/docs/advanced-features/custom-server


  1. Rename server.js to server.ts.

  2. Update the scripts in package.json

   "scripts": {
   "dev": "ts-node server.ts",
   "build": "next build",
   "start": "NODE_ENV=production ts-node server.ts"
   }
Enter fullscreen mode Exit fullscreen mode
  1. Install ts-node
   yarn add ts-node
Enter fullscreen mode Exit fullscreen mode
  1. Add this section in tsconfig.json
   {
     // ...
     "ts-node": {
       "transpileOnly": true,
       "compilerOptions": {
         "module": "commonjs"
       }
     }
   }
Enter fullscreen mode Exit fullscreen mode

Top comments (3)

Collapse
 
arthurka profile image
ArthurKa

And? Wtf? Is that all?

Collapse
 
arthurka profile image
ArthurKa

Loool. Really worked. Thanks!

Collapse
 
masadamsahid profile image
Maulana Adam Sahid

Awesome 🔥