Skip to content

Commit

Permalink
fix(NODE_ENV): Warn when launching start or build on development (#14033
Browse files Browse the repository at this point in the history
)

The PR aims to fix [#14022](#14022)

Co-authored-by: Tim Neutkens <6324199+timneutkens@users.noreply.github.com>
  • Loading branch information
moh12594 and timneutkens committed Dec 7, 2021
1 parent 21f8c6a commit d459b5f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/next/bin/next.ts
Expand Up @@ -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
Expand Down
44 changes: 44 additions & 0 deletions test/integration/non-standard-node-env-warning/test/index.test.js
Expand Up @@ -9,6 +9,7 @@ import {
waitFor,
initNextServerScript,
nextBuild,
nextStart,
} from 'next-test-utils'

const appDir = join(__dirname, '..')
Expand Down Expand Up @@ -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)
})
})

0 comments on commit d459b5f

Please sign in to comment.