Freecodecamp - Next-Sanity Finding React 18 instead of React 17

17 replies
Last updated: Apr 9, 2022
Hello all! I'm working through a freecodecamp sanity + next tutorial (power user right here!) which is going well until I try to use next-sanity. Any help would be greatly appreciated πŸ™‚
I am getting an error (screengrab attached) which I believe relates to @portabletext/react looking for React 17 but finding only React 18 (installed by create-next-app)? Any ideas? Thanks.
AI Update

This is a common peer dependency issue that often pops up when working with Sanity and Next.js, especially when following older tutorials. The good news is it's usually just a warning that won't break your app!

Based on what I found in the Sanity GitHub issues, this happens when there are conflicting peer dependency requirements between different Sanity packages (like @portabletext/react, next-sanity, and @sanity/icons) and the version of React installed by create-next-app.

Here are your options to fix this:

1. Use --legacy-peer-deps (quickest solution)

When you install packages, add this flag:

npm install --legacy-peer-deps

This tells npm to ignore peer dependency conflicts. Despite the warning, the packages will work fine together - React 18 is generally backward compatible with libraries expecting React 17.

2. Update to the latest package versions

If the tutorial is using older versions, try updating to the latest:

npm install next-sanity@latest @portabletext/react@latest sanity@latest --legacy-peer-deps

The latest versions of these packages have been updated to properly support React 18 (and even React 19).

3. Configure npm to always use legacy peer deps

If you want to avoid typing the flag every time:

npm config set legacy-peer-deps true

4. Use --force (less recommended)

npm install --force

This is more aggressive than --legacy-peer-deps but will also bypass the check.

Why this happens:

Many FreeCodeCamp and other tutorials were created when React 17 was current. Package maintainers took time to update their peerDependencies declarations to include React 18, even though the actual code worked fine across versions. npm's strict peer dependency checking (introduced in npm 7+) flags these as potential issues, but they're usually safe to ignore.

According to the GitHub issue thread, even Sanity team members encountered this during npx sanity init - it's a known quirk of the ecosystem transitioning to React 18.

Moving forward:

Your app should run fine even with these warnings. The peer dependency system is being overly cautious. As long as your development server starts and your app renders correctly, you're good to go! The @portabletext/react library works perfectly fine with React 18 despite what the peer dependency declaration might say.

If you continue having runtime errors (not just install warnings), that would be a different issue - but the peer dependency warnings themselves won't prevent your code from working.

it looks like the current package for react-portable-text does support react 18, you could try updating to that by running
npm install react-portable-text@latest
actually hold up I might be looking at wrong package sorry
seems this package does not support react-18 yet, but 17 is only listed as a peer dep. I would suggest possibly switching to react 17 if possible, there are not likely too many things you need in 18 at this point
To do that I go to the package.json in the next app folder and change the react and react dom versions to 17 and then npm install?
naively yes, and it might work but react 18 changes some system level stuff, and that might cause other bits to break, ideally you would use CRA to get a slightly older version.
you can try it and see if it works
another option is to ignore peer dependencies when installing @portabletext/react
as it might work just fine with react 18
Thanks I'll try your suggestions
No dice (assuming I reinstalled portable text correctly, which is a big assumption πŸ˜„)I think the issue is the tutorial may be out of date, all the functions the instructor used are causing Next js to throw errors. (or else initial intuition React 18 is the problem is correct?)
Back to the drawing board
πŸ˜…
that sucks - next js might have a good starting template for you to use
or these: https://www.sanity.io/exchange/type=starters/framework=nextjs - I built (am building) our company framework by starting from/copying a lot of the Next.js Landing Pages template there
I cloned a starter pack and with React 17.0.2 npm run dev worked fine. Updated React and React DOM to 18.0.0 and it's broken with the same error message I was getting in the other project.
Is there a way to create a next app but to flag it to use React 17.0.2 instead of the default 18? Been banging my head against the keyboard for a while now, be a shame to give up on the tutorial now
🀣
next should work just fine with react 17 - why did you upgrade it to 18 (like is there another tool that requires it)? you should be able to edit package.json back down to
^17.0.2
for both
react
and
react-dom
i don't imagine there is much in the tutorial that is v18 specific - but if you shoot me a link to it, I can double check that assertion
When I use npx create-next-app it automatically uses react 18. Sfa of real benefit to me with react 18.
Got it working, thanks for your help yesterday

Sanity – Build the way you think, not the way your CMS thinks

Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.

Was this answer helpful?