Error "Cannot find module 'react' or its corresponding type declarations." inside our sharePoint online SPFx project

john john 946 Reputation points
2022-06-05T14:23:31.303+00:00

I am working on an 1.14 SPFX web part, but inside the .ts file we are getting reference errors on almost all the libraries as shown in this picture:-

For example, on the first reference error "Cannot find module 'react' or its corresponding type declarations... i tried those commands:-

npm install react  
  
npm install --save-dev @types/react  
  
npm i @microsoft/rush-stack-compiler-3.9  

but did not fix anything.. any advice on this please?

Thanks

SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,301 questions
SharePoint Server Development
SharePoint Server Development
SharePoint Server: A family of Microsoft on-premises document management and storage systems.Development: The process of researching, productizing, and refining new or existing technologies.
1,597 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Tong Zhang_MSFT 9,141 Reputation points
    2022-06-06T06:30:00.537+00:00

    Hi @john john ,

    According to my research and testing, please try to set moduleResolution to node in your tsconfig.json file.

    208597-image.png
    If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE. You can run the following command:

    rm -rf node_modules package-lock.json  
      
    npm install  
    

    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.