diff --git a/packages/next/src/executors/server/server.impl.ts b/packages/next/src/executors/server/server.impl.ts index 6532433927dd5..fb6fe89c04070 100644 --- a/packages/next/src/executors/server/server.impl.ts +++ b/packages/next/src/executors/server/server.impl.ts @@ -1,12 +1,10 @@ import 'dotenv/config'; -import * as net from 'net'; import { ExecutorContext, - logger, parseTargetString, readTargetOptions, } from '@nx/devkit'; -import { resolve } from 'path'; +import { join, resolve } from 'path'; import { NextBuildBuilderOptions, @@ -25,15 +23,6 @@ export default async function* serveExecutor( if (options.customServerTarget) { return yield* customServer(options, context); } - // Cast to any to overwrite NODE_ENV - (process.env as any).NODE_ENV = process.env.NODE_ENV - ? process.env.NODE_ENV - : options.dev - ? 'development' - : 'production'; - - // Setting port that the custom server should use. - (process.env as any).PORT = options.port; const buildOptions = readTargetOptions( parseTargetString(options.buildTarget, context.projectGraph), @@ -43,6 +32,19 @@ export default async function* serveExecutor( const { port, keepAliveTimeout, hostname } = options; + // This is required for the default custom server to work. See the @nx/next:app generator. + process.env.NX_NEXT_DIR = root; + + // Cast to any to overwrite NODE_ENV + (process.env as any).NODE_ENV = process.env.NODE_ENV + ? process.env.NODE_ENV + : options.dev + ? 'development' + : 'production'; + + // Setting port that the custom server should use. + process.env.PORT = `${options.port}`; + const args = createCliOptions({ port, keepAliveTimeout, hostname }); const nextDir = resolve(context.root, buildOptions.outputPath);