From 23a06c6493026058923451b4ae7445404f88ea91 Mon Sep 17 00:00:00 2001 From: Nicholas Cunningham Date: Tue, 20 Aug 2024 11:43:28 -0600 Subject: [PATCH] fix(nextjs): should not fail when running outside of nx cli (#27523) fixes: #22450 ## Current Behavior Currently, if you run any next target outside of nx the build will fail because we only fallback for graph creation of if running prod server. ## Expected Behavior Targets should work if they are run outside of the Nx cli ## Related Issue(s) Fixes # (cherry picked from commit 31c57451262daf185e093cecd5c59cb1e12fc3da) --- packages/next/plugins/with-nx.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/next/plugins/with-nx.ts b/packages/next/plugins/with-nx.ts index 49f1dca9b0d2d..cb963ce444613 100644 --- a/packages/next/plugins/with-nx.ts +++ b/packages/next/plugins/with-nx.ts @@ -121,12 +121,17 @@ function withNx( const { PHASE_PRODUCTION_SERVER, PHASE_DEVELOPMENT_SERVER } = await import( 'next/constants' ); - // Two scenarios where we want to skip graph creation: + // Three scenarios where we want to skip graph creation: // 1. Running production server means the build is already done so we just need to start the Next.js server. // 2. During graph creation (i.e. create nodes), we won't have a graph to read, and it is not needed anyway since it's a build-time concern. + // 3. Running outside of Nx, we don't have a graph to read. // // NOTE: Avoid any `require(...)` or `import(...)` statements here. Development dependencies are not available at production runtime. - if (PHASE_PRODUCTION_SERVER === phase || global.NX_GRAPH_CREATION) { + if ( + PHASE_PRODUCTION_SERVER === phase || + global.NX_GRAPH_CREATION || + !process.env.NX_TASK_TARGET_TARGET + ) { const { nx, ...validNextConfig } = _nextConfig; return { distDir: '.next',