diff --git a/packages/next/bin/next.ts b/packages/next/bin/next.ts index a2169a8ce16eb2b..e8df92a3c8ae887 100755 --- a/packages/next/bin/next.ts +++ b/packages/next/bin/next.ts @@ -88,8 +88,18 @@ const defaultEnv = command === 'dev' ? 'development' : 'production' const standardEnv = ['production', 'development', 'test'] -if (process.env.NODE_ENV && !standardEnv.includes(process.env.NODE_ENV)) { - log.warn(NON_STANDARD_NODE_ENV) +if (process.env.NODE_ENV) { + const isNotStandard = !standardEnv.includes(process.env.NODE_ENV) + const shouldWarnCommands = + process.env.NODE_ENV === 'development' + ? ['start', 'build'] + : process.env.NODE_ENV === 'production' + ? ['dev'] + : [] + + if (isNotStandard || shouldWarnCommands.includes(command)) { + log.warn(NON_STANDARD_NODE_ENV) + } } ;(process.env as any).NODE_ENV = process.env.NODE_ENV || defaultEnv diff --git a/test/integration/non-standard-node-env-warning/test/index.test.js b/test/integration/non-standard-node-env-warning/test/index.test.js index 5a27946ea175444..6c36b0b07f8de5e 100644 --- a/test/integration/non-standard-node-env-warning/test/index.test.js +++ b/test/integration/non-standard-node-env-warning/test/index.test.js @@ -9,6 +9,7 @@ import { waitFor, initNextServerScript, nextBuild, + nextStart, } from 'next-test-utils' const appDir = join(__dirname, '..') @@ -135,4 +136,47 @@ describe('Non-Standard NODE_ENV', () => { await killApp(app) expect(output).toContain(warningText) }) + + it('should show the warning with NODE_ENV set to production with next dev', async () => { + let output = '' + + app = await launchApp(appDir, await findPort(), { + env: { + NODE_ENV: 'production', + }, + onStderr(msg) { + output += msg || '' + }, + }) + await waitFor(2000) + await killApp(app) + expect(output).toContain(warningText) + }) + + it('should show the warning with NODE_ENV set to development with next build', async () => { + const { stderr } = await nextBuild(appDir, [], { + env: { + NODE_ENV: 'development', + }, + stderr: true, + }) + expect(stderr).toContain(warningText) + }) + + it('should show the warning with NODE_ENV set to development with next start', async () => { + let output = '' + + await nextBuild(appDir) + app = await nextStart(appDir, await findPort(), { + env: { + NODE_ENV: 'development', + }, + onStderr(msg) { + output += msg || '' + }, + }) + await waitFor(2000) + await killApp(app) + expect(output).toContain(warningText) + }) })