Error: Cannot find module "react" in Gatsby

During the migration of this site from Jekyll to Gatsby and switching hosting to Azure Static Web Apps, the only Gatsby related issue I experienced was:

ERROR

There was a problem loading the local develop command. Gatsby may not be installed. Perhaps you need to run “npm install”? Cannot find module ‘react’

This issue occured after I added a new plugin to handle gatsby redirects.

The issue appears because of conflicting versions of React and npm unable to reconcile them individually.

For most people, the solution was to clean out all node_modules and reinstall:

rm -Rf node_modules package-lock.json
npm install

If you still experience issues after trying the above, try swapping to yarn.

rm -Rf node_modules package-lock.json
yarn install

Incidentally, Azure Static Web Apps uses yarn, so I swapped over permanently for this project.

About the author

For the past two decades, Scott McCulloch has worked with a variety of web technologies. He is currently focused on full-stack development. On his day to day job, he is working as a systems specialist at the NSW State Emergency Service.