Package Exports
- nx-next-server
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (nx-next-server) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Nx Next Server
Utility for creating a custom Next.js server with Nx in a separate app.
Creating Next.js custom servers with Nx is not possible (as of the time this package was created). nx-next-server provides a simple utility function which can be used to start a Next.js app within Nx from another Nx app.
The benefits of this are:
- Create custom Next.js server with TypeScript in Nx
- Import and use Nx libs within custom server
Install
$ yarn add nx-next-server
# or
$ npm install nx-next-serverUsage
Your NX workspace should include a Next.js app and a server app (Node.js/Express).
This example assumes you have a Nx workspace with two apps:
- apps/web - Next.js app
- apps/server - Express app
// apps/server/src/main.ts
import createServer from "nx-next-server";
import * as express from "express";
// Create Next.js server for apps/web app
createServer("web").then(({ nextApp, handler, settings, proxyConfig }) => {
const server = express();
server.all("*", (req, res) => handler(req, res));
server.listen(settings.port, settings.hostname, () => {
console.log(
`Listening on ${settings.hostname || "http://localhost"}:${settings.port}`
);
});
});Once you've written your express app, it can be run using:
$ yarn start serverBuild
Your Next.js app can be built with:
$ yarn build webYour Express server can be built with:
$ yarn build server --configuration=productionNote: you need to include
--configuration=productionto ensure your server targets thedist/apps/webNext.js build.
The build can be tested by running the main.js file in dist:
$ node ./dist/apps/server/main.js