Report this

What is the reason for this report?

Cannot find module './serviceWorker' or its corresponding type declarations

Posted on February 25, 2021

Hi First of all thanks for the informative post made and it was really helpful for me as a newbie for react and typescript. This could be a silly question that as I gone through the tutorial (https://www.digitalocean.com/community/tutorials/how-to-build-a-customer-list-management-app-with-react-and-typescript) and while trying to run the program it came a issue that “Cannot find module ‘./serviceWorker’ or its corresponding type declarations” in typescript-react-app/src/index.tsx, I struggle a lot to find a solution yet not succeed. Can you help me on this point. Following are the complete error message.

D:/UserData/reactPractice/reactApp4/typescript-react-app/src/index.tsx TypeScript error in D:/UserData/reactPractice/reactApp4/typescript-react-app/src/index.tsx(6,32): Cannot find module ‘./serviceWorker’ or its corresponding type declarations. TS2307

4 | import App from './App';
5 | import { BrowserRouter } from 'react-router-dom'; 

6 | import * as serviceWorker from ‘./serviceWorker’; | ^ 7 | ReactDOM.render( 8 | <BrowserRouter> 9 | <App />



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Heya,

Just came across this answer and decided to write some general guidelines for anyone who comes across this in the future despite the old question.

The error you’re seeing suggests that TypeScript can’t locate a module named serviceWorker in your project. This often happens when the serviceWorker.ts file is missing from your project directory or if the import path specified in index.tsx is incorrect.

In Create React App versions 4 and onward, a file named service-worker.ts is present instead of serviceWorker.ts. If you’re using this version or above, you should change your import statement to: import * as serviceWorker from ‘./service-worker’;

Otherwise, if you’re following the tutorial strictly and you don’t want to use a service worker, you can remove that import line completely.

Hope that this helps!

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.