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:
- Created 6 years ago
- Comments:13 (10 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Don’t use older TypeScript + Next.js boilerplates.
Continue running the custom server like you used to, you can use
nodemon
withts-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.Use
import * as express from 'express'; import * as next from 'next';
or set"esModuleInterop": true
will fixTypeError: next_1.default is not a function
.