Search code examples
javascriptreactjsweb-applicationscreate-react-app

Unable to compile react application as it says "Module not found"


I have been trying to execute a very simple react application, but for some reason I'm unable to compile it. When i try to compile it in bash using "npm start", i get the error stating "Failed to compile: ./src/index.js Module not found: Can't resolve './registerServiceWorker' in '/Users/Pruthvi/Desktop/ReactApplication/my-app/src'"

can anybody help me as I am a new bee to react application!


Solution

  • By default, create-react-app will create a progressive web app, as documented here. The progressive web app relies on registerServiceWorker() in the registerServiceWorker.js file. You have apparently deleted this file, so your project no longer builds.

    You have two choices:

    1. Keep your project as a progressive web app, in which case you should simply restore the original state you had after first creating.
    2. Opt out of the progressive web app features.

    In order to opt out of the progressive web app features, you need to modify your src/index.js file as follows:

    1. Delete the import of the registerServiceWorker.
    2. Delete the registerServiceWorker() call.

    Your src/index.js will then look like this:

    import React from 'react';
    import ReactDOM from 'react-dom';
    import './index.css';
    import App from './App';
    // Deleted -- import registerServiceWorker from './registerServiceWorker';
    
    ReactDOM.render(<App />, document.getElementById('root'));
    // Deleted -- registerServiceWorker();
    

    Once you've made those modifications to your index.js, you can safely delete the registerServiceWorker.js file (which it appears you've already done based on the error message you've posted).

    The documentation gives a more complete description of how to opt out and some of the consequences of having a progressive web app.