Skip to content

Commit f861e0d

Browse files
authored
Make sure not to require react before NODE_ENV has been set (#7200)
* Make sure not to require react before NODE_ENV has been set * Update to use force kill to make windows happy
1 parent ab22b58 commit f861e0d

File tree

4 files changed

+70
-8
lines changed

4 files changed

+70
-8
lines changed

packages/next/bin/next.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@ import arg from 'next/dist/compiled/arg/index.js'
1111
}
1212
})
1313

14-
const React = require('react')
15-
16-
if (typeof React.Suspense === 'undefined') {
17-
throw new Error(`The version of React you are using is lower than the minimum required version needed for Next.js. Please upgrade "react" and "react-dom": "npm install --save react react-dom" https://err.sh/zeit/next.js/invalid-react-version`)
18-
}
19-
2014
const defaultCommand = 'dev'
2115
export type cliCommand = (argv?: string[]) => void
2216
const commands: {[command: string]: () => Promise<cliCommand>} = {
@@ -84,6 +78,14 @@ if (args['--help']) {
8478
const defaultEnv = command === 'dev' ? 'development' : 'production'
8579
process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv
8680

81+
// this needs to come after we set the correct NODE_ENV or
82+
// else it might cause SSR to break
83+
const React = require('react')
84+
85+
if (typeof React.Suspense === 'undefined') {
86+
throw new Error(`The version of React you are using is lower than the minimum required version needed for Next.js. Please upgrade "react" and "react-dom": "npm install --save react react-dom" https://err.sh/zeit/next.js/invalid-react-version`)
87+
}
88+
8789
commands[command]().then((exec) => exec(forwardedArgs))
8890

8991
if (command === 'dev') {
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import React from 'react'
2+
3+
const Idk = React.createContext(null)
4+
5+
export default () => {
6+
return (
7+
<div>
8+
<Idk.Provider value='hello world'>
9+
<Idk.Consumer>
10+
{(idk) => (
11+
<p>Value: {idk}</p>
12+
)}
13+
</Idk.Consumer>
14+
</Idk.Provider>
15+
</div>
16+
)
17+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* eslint-env jest */
2+
/* global jasmine */
3+
import { join } from 'path'
4+
import {
5+
killApp,
6+
findPort,
7+
runNextCommand,
8+
renderViaHTTP
9+
} from 'next-test-utils'
10+
11+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 30
12+
13+
const appDir = join(__dirname, '../')
14+
let appPort
15+
let app
16+
17+
describe('Production Usage', () => {
18+
beforeAll(async () => {
19+
await runNextCommand(['build', appDir])
20+
})
21+
22+
it('should render a page with context', async () => {
23+
appPort = await findPort()
24+
25+
await new Promise((resolve, reject) => {
26+
runNextCommand(['start', appDir, '-p', appPort], {
27+
instance: (child) => {
28+
app = child
29+
child.stdout.on('data', chunk => {
30+
if (chunk.toString().match(/ready on/i)) resolve()
31+
})
32+
child.stderr.on('data',
33+
chunk => reject(new Error('got error ' + chunk.toString()))
34+
)
35+
}
36+
}).catch(err => reject(err))
37+
})
38+
39+
const html = await renderViaHTTP(appPort, '/')
40+
expect(html).toMatch(/Value: .*?hello world/)
41+
await killApp(app)
42+
})
43+
})

test/lib/next-test-utils.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export function runNextCommand (argv, options = {}) {
7575
const nextBin = path.join(nextDir, 'dist/bin/next')
7676
const cwd = options.cwd || nextDir
7777
// Let Next.js decide the environment
78-
const env = { ...process.env, ...options.env, NODE_ENV: undefined }
78+
const env = { ...process.env, ...options.env, NODE_ENV: '' }
7979

8080
return new Promise((resolve, reject) => {
8181
console.log(`Running command "next ${argv.join(' ')}"`)
@@ -162,7 +162,7 @@ export function nextExport (dir, { outdir }) {
162162

163163
// Kill a launched app
164164
export async function killApp (instance) {
165-
await fkill(instance.pid)
165+
await fkill(instance.pid, { force: true })
166166
}
167167

168168
export async function startApp (app) {

0 commit comments

Comments
 (0)