question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

how to run a custom express server with typescript and nextjs?

See original GitHub issue
{
  // package.json
  "name": "with-typescript",
  "version": "1.0.0",
  "scripts": {
    "dev": "concurrently \"tsc --watch\" && node server.js"
  },
  "dependencies": {
    "express": "^4.15.4",
    "express-logging": "^1.1.1",
    "logops": "^2.1.0",
    "next": "latest",
    "react": "^15.5.4",
    "react-dom": "^15.5.4"
  },
  "devDependencies": {
    "@types/node": "^8.0.20",
    "@types/react": "^15.0.1",
    "babel-plugin-root-import": "^5.1.0",
    "babel-preset-es2015": "^6.24.1",
    "babel-register": "^6.24.1",
    "concurrently": "^3.1.0",
    "typescript": "^2.1.5"
  }
}
// server.ts
import express from 'express';
import next from 'next';

const dev = process.env.NODE_ENV !== 'production';
const port = process.env.PORT || 3000;
const app = next({ dev });
const handle = app.getRequestHandler();

app.prepare()
  .then(() => {
    const server = express();

    server.get('*', (req, res) => {
      return handle(req, res);
    });

    server.listen(port, (err) => {
      if (err) throw err;
      console.log(`> Ready on http://localhost:${port}`);
    });
  });

If I run npm run dev nothing really happens.

If I instead run change the script to "dev": "node server.js"

I get an error TypeError: next_1.default is not a function

Since server.ts is being converted to server.js it gives me this line where this error is: const app = next_1.default({ dev }); from const app = next({ dev });

So question is, how can I run custom express server using typescript?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:13 (10 by maintainers)

github_iconTop GitHub Comments

6reactions
retrixecommented, Feb 8, 2018

Don’t use older TypeScript + Next.js boilerplates.

Continue running the custom server like you used to, you can use nodemon with ts-node to run it and with Next.js 5, use @zeit/next-typescript along with your custom server in combination with @zeit/next-css or whatever you happen to require.

And get rid of concurrently. Just start the custom server and next-typescript or your custom ts-loader configuration in next.config.js will keep you good to go.

2reactions
robertu7commented, Feb 9, 2019

Use import * as express from 'express'; import * as next from 'next'; or set "esModuleInterop": true will fix TypeError: next_1.default is not a function.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Set Up Next.js with a Custom Express Server + Typescript
In this post, I will walk you through how to make a Next.js application handled by a custom Express server with Typescript.
Read more >
Custom NextJS Server with Typescript - DEV Community ‍ ‍
There is an official NextJS guide on how to create a custom server. However, it only works with JavaScript. Please follow their guide...
Read more >
Next.js Custom Server Typescript Example - StackBlitz
Run official live example code for Next.js Custom Server Typescript, created by Vercel on StackBlitz.
Read more >
Advanced Features: Custom Server - Next.js
A custom Next.js server allows you to start a server 100% programmatically in order to use custom server patterns. Most of the time,...
Read more >
Combine Typescript with NextJS and Express - Medium
Go back at server.tsx file. import hi.tsx and use this controller at app block scope. the complete file should look like this.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found