Skip to content

Commit

Permalink
Add checking of react versions (vercel#6892)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ijjk authored Apr 4, 2019
1 parent 682b3ed commit 9995f5d
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 3 deletions.
6 changes: 3 additions & 3 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ jobs:
JEST_JUNIT_CLASSNAME: '{filepath}'
- store_test_results:
path: ~/repo/reports
test-production:
test-ie11:
docker:
- image: circleci/node:8-browsers
working_directory: ~/repo
steps:
- attach_workspace:
at: .
- run:
name: Production Tests
name: Test in ie11
command: 'if [[ ! -z $BROWSERSTACK_USERNAME ]]; then yarn testall test/integration/production/; else echo "Not running for PR"; fi'
environment:
BROWSERSTACK: 'true'
Expand Down Expand Up @@ -68,7 +68,7 @@ workflows:
- test:
requires:
- build
- test-production:
- test-ie11:
requires:
- build
- deploy:
Expand Down
9 changes: 9 additions & 0 deletions errors/invalid-react-version.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Invalid React Version

#### Why This Error Occurred

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.

#### Possible Ways to Fix It

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.
6 changes: 6 additions & 0 deletions packages/next/bin/next.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import arg from 'next/dist/compiled/arg/index.js'
}
})

const React = require('react')

if (typeof React.Suspense === 'undefined') {
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`)
}

const defaultCommand = 'dev'
export type cliCommand = (argv?: string[]) => void
const commands: {[command: string]: () => Promise<cliCommand>} = {
Expand Down
6 changes: 6 additions & 0 deletions packages/next/server/next-dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import ErrorDebug from './error-debug'
import AmpHtmlValidator from 'amphtml-validator'
import { ampValidation } from '../build/output/index'

const React = require('react')

if (typeof React.Suspense === 'undefined') {
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`)
}

export default class DevServer extends Server {
constructor (options) {
super(options)
Expand Down
15 changes: 15 additions & 0 deletions test/unit/handles-incorrect-react.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/* eslint-env jest */
import path from 'path'

jest.mock('react', () => ({
Suspense: undefined
}))

const nextDir = path.dirname(require.resolve('next/package'))
const nextBin = path.join(nextDir, 'dist/bin/next')

describe('Handles Incorrect React Version', () => {
it('should throw an error when building with next', async () => {
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/)
})
})

0 comments on commit 9995f5d

Please sign in to comment.