Unverified Commit 8b9aa2dc authored by Zack Tanner's avatar Zack Tanner Committed by GitHub

add experimental flag to allow forcing NODE_ENV=development in builds (#65463)

For debugging purposes, it can be useful to set `NODE_ENV=development`
during a `next build`. Currently this value is forced to be production
in Next.js. This PR adds an experimental flag to not force a mode of
`production` when the flag is set.

To use this flag, you'll still need to explicitly set
`NODE_ENV=development`, while also enabling
`nextConfig.experimental.allowDevelopmentBuild`

Closes NEXT-3277
parent d0d22ac6
...@@ -117,7 +117,9 @@ async function loaderTransform( ...@@ -117,7 +117,9 @@ async function loaderTransform(
filename, filename,
isServer, isServer,
isPageFile, isPageFile,
development: this.mode === 'development', development:
this.mode === 'development' ||
!!nextConfig.experimental?.allowDevelopmentBuild,
hasReactRefresh, hasReactRefresh,
modularizeImports: nextConfig?.modularizeImports, modularizeImports: nextConfig?.modularizeImports,
optimizePackageImports: nextConfig?.experimental?.optimizePackageImports, optimizePackageImports: nextConfig?.experimental?.optimizePackageImports,
......
...@@ -159,7 +159,10 @@ export function getDefineEnv({ ...@@ -159,7 +159,10 @@ export function getDefineEnv({
'process.turbopack': isTurbopack, 'process.turbopack': isTurbopack,
'process.env.TURBOPACK': isTurbopack, 'process.env.TURBOPACK': isTurbopack,
// TODO: enforce `NODE_ENV` on `process.env`, and add a test: // TODO: enforce `NODE_ENV` on `process.env`, and add a test:
'process.env.NODE_ENV': dev ? 'development' : 'production', 'process.env.NODE_ENV':
dev || config.experimental.allowDevelopmentBuild
? 'development'
: 'production',
'process.env.NEXT_RUNTIME': isEdgeServer 'process.env.NEXT_RUNTIME': isEdgeServer
? 'edge' ? 'edge'
: isNodeServer : isNodeServer
......
...@@ -421,6 +421,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() => ...@@ -421,6 +421,7 @@ export const configSchema: zod.ZodType<NextConfig> = z.lazy(() =>
useEarlyImport: z.boolean().optional(), useEarlyImport: z.boolean().optional(),
testProxy: z.boolean().optional(), testProxy: z.boolean().optional(),
defaultTestRunner: z.enum(SUPPORTED_TEST_RUNNERS_LIST).optional(), defaultTestRunner: z.enum(SUPPORTED_TEST_RUNNERS_LIST).optional(),
allowDevelopmentBuild: z.literal(true).optional(),
}) })
.optional(), .optional(),
exportPathMap: z exportPathMap: z
......
...@@ -452,6 +452,10 @@ export interface ExperimentalConfig { ...@@ -452,6 +452,10 @@ export interface ExperimentalConfig {
* Set a default test runner to be used by `next experimental-test`. * Set a default test runner to be used by `next experimental-test`.
*/ */
defaultTestRunner?: SupportedTestRunners defaultTestRunner?: SupportedTestRunners
/**
* Allow NODE_ENV=development even for `next build`.
*/
allowDevelopmentBuild?: true
} }
export type ExportPathMap = { export type ExportPathMap = {
...@@ -963,6 +967,7 @@ export const defaultConfig: NextConfig = { ...@@ -963,6 +967,7 @@ export const defaultConfig: NextConfig = {
dynamic: 30, dynamic: 30,
static: 300, static: 300,
}, },
allowDevelopmentBuild: undefined,
}, },
bundlePagesRouterDependencies: false, bundlePagesRouterDependencies: false,
} }
......
...@@ -262,6 +262,15 @@ function assignDefaults( ...@@ -262,6 +262,15 @@ function assignDefaults(
const result = { ...defaultConfig, ...config } const result = { ...defaultConfig, ...config }
if (
result.experimental?.allowDevelopmentBuild &&
process.env.NODE_ENV !== 'development'
) {
throw new Error(
`The experimental.allowDevelopmentBuild option requires NODE_ENV to be explicitly set to 'development'.`
)
}
if ( if (
result.experimental?.ppr && result.experimental?.ppr &&
!process.env.__NEXT_VERSION!.includes('canary') && !process.env.__NEXT_VERSION!.includes('canary') &&
......
import { nextTestSetup } from 'e2e-utils'
import { retry } from 'next-test-utils'
describe('allow-development-build', () => {
describe('with NODE_ENV set to development', () => {
const { next } = nextTestSetup({
files: __dirname,
env: {
NODE_ENV: 'development',
},
nextConfig: {
experimental: {
allowDevelopmentBuild: true,
},
},
})
it('should warn about a non-standard NODE_ENV', () => {
expect(next.cliOutput).toContain(
'You are using a non-standard "NODE_ENV" value in your environment'
)
})
it.each(['app-page', 'pages-page'])(
`should show React development errors in %s`,
async (page) => {
const browser = await next.browser(page, {
pushErrorAsConsoleLog: true,
})
await retry(async () => {
const logs = await browser.log()
const errorLogs = logs.filter((log) => log.source === 'error')
expect(errorLogs).toEqual(
expect.arrayContaining([
{
message: expect.toBeOneOf([
expect.stringContaining(
"Hydration failed because the server rendered HTML didn't match the client. As a result this tree will be regenerated on the client."
),
expect.stringContaining(
'There was an error while hydrating. Because the error happened outside of a Suspense boundary, the entire root will switch to client rendering.'
),
]),
source: 'error',
},
])
)
})
}
)
})
describe('with NODE_ENV not set to development', () => {
const { next } = nextTestSetup({
files: __dirname,
skipStart: true,
nextConfig: {
experimental: {
allowDevelopmentBuild: true,
},
},
})
it('should fail the build with a message about not setting NODE_ENV', async () => {
await next.start().catch(() => {})
expect(next.cliOutput).toContain(
"The experimental.allowDevelopmentBuild option requires NODE_ENV to be explicitly set to 'development'"
)
})
})
})
'use client'
import React from 'react'
export default function Page() {
return (
<div>
Hello World{' '}
{typeof window !== 'undefined' && <span>Hydration Error!</span>}
</div>
)
}
export default function RootLayout({
children,
}: {
children: React.ReactNode
}) {
return (
<html lang="en">
<body>{children}</body>
</html>
)
}
export default function Page() {
return (
<div>
Hello World{' '}
{typeof window !== 'undefined' && <span>Hydration Error!</span>}
</div>
)
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment