diff --git a/packages/next/server/lib/utils.ts b/packages/next/server/lib/utils.ts index f8f4ae1303496..e8830a9576610 100644 --- a/packages/next/server/lib/utils.ts +++ b/packages/next/server/lib/utils.ts @@ -8,3 +8,8 @@ export function printAndExit(message: string, code = 1) { process.exit(code) } + +export function getNodeOptionsWithoutInspect() { + const NODE_INSPECT_RE = /--inspect(-brk)?(=\S+)? ?/ + return (process.env.NODE_OPTIONS || '').replace(NODE_INSPECT_RE, '') +} diff --git a/packages/next/server/next-dev-server.ts b/packages/next/server/next-dev-server.ts index ff97701a1f611..82c9dbeebfb7a 100644 --- a/packages/next/server/next-dev-server.ts +++ b/packages/next/server/next-dev-server.ts @@ -31,6 +31,7 @@ import ErrorDebug from './error-debug' import HotReloader from './hot-reloader' import { findPageFile } from './lib/find-page-file' import Worker from 'jest-worker' +import { getNodeOptionsWithoutInspect } from './lib/utils' if (typeof React.Suspense === 'undefined') { throw new Error( diff --git a/test/integration/cli/test/index.test.js b/test/integration/cli/test/index.test.js index 9014b11b7911e..6208cfe1522a2 100644 --- a/test/integration/cli/test/index.test.js +++ b/test/integration/cli/test/index.test.js @@ -88,6 +88,16 @@ describe('CLI Usage', () => { expect(output).toMatch(new RegExp(`http://localhost:${port}`)) }) + test("NODE_OPTIONS='--inspect'", async () => { + // this test checks that --inspect works by launching a single debugger for the main Next.js process, + // not for its subprocesses + const port = await findPort() + const output = await runNextCommandDev([dir, '--port', port], true, { + env: { NODE_OPTIONS: '--inspect' }, + }) + expect(output).toMatch(new RegExp(`http://localhost:${port}`)) + }) + test('-p', async () => { const port = await findPort() const output = await runNextCommandDev([dir, '-p', port], true)