Skip to content

Commit 9995f5d

Browse files
authored
Add checking of react versions (vercel#6892)
* Add checking of react versions to make sure it meets the minimum set in peerDependencies * Simplify react check * Update error wording Co-Authored-By: timneutkens <[email protected]> * Add err.sh * Update test-production circleci job name * Add react error message to next-dev-server * Update test for new wording
1 parent 682b3ed commit 9995f5d

File tree

5 files changed

+39
-3
lines changed

5 files changed

+39
-3
lines changed

.circleci/config.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ jobs:
3131
JEST_JUNIT_CLASSNAME: '{filepath}'
3232
- store_test_results:
3333
path: ~/repo/reports
34-
test-production:
34+
test-ie11:
3535
docker:
3636
- image: circleci/node:8-browsers
3737
working_directory: ~/repo
3838
steps:
3939
- attach_workspace:
4040
at: .
4141
- run:
42-
name: Production Tests
42+
name: Test in ie11
4343
command: 'if [[ ! -z $BROWSERSTACK_USERNAME ]]; then yarn testall test/integration/production/; else echo "Not running for PR"; fi'
4444
environment:
4545
BROWSERSTACK: 'true'
@@ -68,7 +68,7 @@ workflows:
6868
- test:
6969
requires:
7070
- build
71-
- test-production:
71+
- test-ie11:
7272
requires:
7373
- build
7474
- deploy:

errors/invalid-react-version.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Invalid React Version
2+
3+
#### Why This Error Occurred
4+
5+
You tried running `next` in a project with an incompatible react version. Next.js uses certain react features that when are unavailable show this error since it can't work without them.
6+
7+
#### Possible Ways to Fix It
8+
9+
Run `npm i react@latest react-dom@latest` or `yarn add react@latest react-dom@latest` in your project and then try running `next` again.

packages/next/bin/next.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ 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+
1420
const defaultCommand = 'dev'
1521
export type cliCommand = (argv?: string[]) => void
1622
const commands: {[command: string]: () => Promise<cliCommand>} = {

packages/next/server/next-dev-server.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ import ErrorDebug from './error-debug'
77
import AmpHtmlValidator from 'amphtml-validator'
88
import { ampValidation } from '../build/output/index'
99

10+
const React = require('react')
11+
12+
if (typeof React.Suspense === 'undefined') {
13+
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`)
14+
}
15+
1016
export default class DevServer extends Server {
1117
constructor (options) {
1218
super(options)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* eslint-env jest */
2+
import path from 'path'
3+
4+
jest.mock('react', () => ({
5+
Suspense: undefined
6+
}))
7+
8+
const nextDir = path.dirname(require.resolve('next/package'))
9+
const nextBin = path.join(nextDir, 'dist/bin/next')
10+
11+
describe('Handles Incorrect React Version', () => {
12+
it('should throw an error when building with next', async () => {
13+
expect(() => require(nextBin)).toThrow(/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/)
14+
})
15+
})

0 commit comments

Comments
 (0)