On a brand new next.js project using create-next-app, introducing a hook causes the app to crash and the Error: Invalid hook call. Hooks can only be called inside of the body of a function component error.
yarn dev successfully starts the app
next dev causes this error
cd hook-test
Modify pages/index.js to include a hook
import React, { useState } from 'react'
const Home = () => {
const [myVar, setMyVar] = useState()
return <div>hook test</div>
}
export default Home
It should work as expected
[email protected]
[email protected]
[email protected]
[email protected]
Okay for some reason when I start the app up with next dev it fails, but if I use yarn dev it works
It sounds like you are installing Next.js globally, this isn't supported and should be run locally from your project with yarn next dev or yarn dev if you've configured the dev script in your package.json to next dev as you mentioned
Resolved. Thank you @ijjk .
Most helpful comment
It sounds like you are installing Next.js globally, this isn't supported and should be run locally from your project with
yarn next devoryarn devif you've configured thedevscript in yourpackage.jsontonext devas you mentioned