diff --git a/packages/next/src/executors/server/custom-server.impl.ts b/packages/next/src/executors/server/custom-server.impl.ts index e6ec5f795a94f..ea61f4b4cb34c 100644 --- a/packages/next/src/executors/server/custom-server.impl.ts +++ b/packages/next/src/executors/server/custom-server.impl.ts @@ -15,7 +15,8 @@ export default async function* serveExecutor( : 'production'; // Setting port that the custom server should use. - (process.env as any).PORT = options.port; + process.env.PORT = options.port ? `${options.port}` : process.env.PORT; + options.port = parseInt(process.env.PORT); const projectRoot = context.projectGraph.nodes[context.projectName].data.root; diff --git a/packages/next/src/executors/server/server.impl.ts b/packages/next/src/executors/server/server.impl.ts index f770dfa245273..d87ffa469b25d 100644 --- a/packages/next/src/executors/server/server.impl.ts +++ b/packages/next/src/executors/server/server.impl.ts @@ -29,7 +29,7 @@ export default async function* serveExecutor( ); const projectRoot = context.workspace.projects[context.projectName].root; - const { port, keepAliveTimeout, hostname } = options; + const { keepAliveTimeout, hostname } = options; // This is required for the default custom server to work. See the @nx/next:app generator. process.env.NX_NEXT_DIR = projectRoot; @@ -42,9 +42,10 @@ export default async function* serveExecutor( : 'production'; // Setting port that the custom server should use. - process.env.PORT = `${options.port}`; + process.env.PORT = options.port ? `${options.port}` : process.env.PORT; + options.port = parseInt(process.env.PORT); - const args = createCliOptions({ port, hostname }); + const args = createCliOptions({ port: options.port, hostname }); if (keepAliveTimeout && !options.dev) { args.push(`--keepAliveTimeout=${keepAliveTimeout}`); @@ -81,11 +82,11 @@ export default async function* serveExecutor( process.on('SIGTERM', () => killServer()); process.on('SIGHUP', () => killServer()); - await waitForPortOpen(port, { host: options.hostname }); + await waitForPortOpen(options.port, { host: options.hostname }); next({ success: true, - baseUrl: `http://${options.hostname ?? 'localhost'}:${port}`, + baseUrl: `http://${options.hostname ?? 'localhost'}:${options.port}`, }); } );