React create app hot reload is not always working on linux

后端 未结 9 1833
面向向阳花
面向向阳花 2020-12-24 12:03

I created a react application using create-react-app boilerplate, which seems to be very popular, hot reload some times updates when any of the files changes and some times

相关标签:
9条回答
  • 2020-12-24 12:35

    When npm start doesn’t detect changes, below are the common troubleshooting steps provided in the create-react-app documentation - link.

    While an app is running with npm start and updating code in the editor should possibly refresh the borswer with the updated code. If this doesn’t happen, try one of the following workarounds:

    1. If the project files are directly synced to your local system from a cloud storage like Dropbox or Google Drive and your trying to run the app in them directly, try moving it out.
    2. Due to Webpack bug, you may need to restart the watcher. If the watcher doesn’t detect the index.js and you’re referencing it by the folder name.
    3. Safe write feature in editors like Vim and IntelliJ currently breaks the watcher. You will need to disable it.
    4. Due to Webpack watcher bug, projects with path contains parentheses causes issue, try moving the project to a path without them. .
    5. To allow more watchers in Linux and macOS, you might need to tweak system settings.
    6. If the project runs inside a virtual machine such as (a Vagrant provisioned) VirtualBox, create an .env file in your project directory if it doesn’t exist, and add CHOKIDAR_USEPOLLING=true to it. This ensures that the next time you run npm start, the watcher uses the polling mode, as necessary inside a VM.
    7. Could try increasing max_users_watches- link

    More references:

    • Issue Tracker 1
    • Toubleshooting webpack
    • Issue Tracker 2 - Webpack polling
    0 讨论(0)
  • 2020-12-24 12:43

    In unbuntu, i basically kill all the process running on port (for react app default is :3000).

    List all the process running on port 3000.

    lsof -i :3000

    This command will show something like this.

    COMMAND   PID USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME
    node     7429 yipl   19u  IPv4 1081760      0t0  TCP localhost:3000->localhost:35762 (ESTABLISHED)
    chrome  26448 yipl  177u  IPv4 1080082      0t0  TCP localhost:35762->localhost:3000 (ESTABLISHED)
    

    Now kill the process by PID.

    kill -9 7429
    kill -9 26488
    

    Start your react app again.

    0 讨论(0)
  • 2020-12-24 12:44

    Try these:

    • Turn off safe write in your IDE
    • Increase max_user_watches
    • Your path should not have parentheses

    as last resort try to use this as your env variable: CHOKIDAR_USEPOLLING=true npm start

    Sources: https://github.com/facebookincubator/create-react-app/issues/659 https://github.com/facebookincubator/create-react-app/issues/1049#issuecomment-261731734

    0 讨论(0)
  • 2020-12-24 12:45

    One additional case I just experience is when using multiple version of nodejs with NVM in parallel. Basically, I have two terminal windows, one run node 10.x, other on node 9.x, and Webpack watcher stops seeing change.

    The solution is to bring both to the same node version

    0 讨论(0)
  • 2020-12-24 12:51

    sudo npm start

    i dont know more about linux, but it is probably due to firewall settings, as i have done the same in windows and there to start react dev server we need to give the access through firewall, so to do the same here we need to give it the admin permision(sudo in case of a ubuntu linux distribution).

    0 讨论(0)
  • 2020-12-24 12:53

    Apparently hot module reloading only works out-of-the-box if you eject your app.

    But if you haven't ejected your app, then you can follow these instructions to get it working.

    Find the ReactDOM.render(...) at the top of your app, and add these lines below it:

    ReactDOM.render(...);
    
    if (module.hot) {
      module.hot.accept('./App', () => {
        // --- Copy-paste your usual ReactDOM.render(...) call here: --- //
        ReactDOM.render(...);
      });
    }
    

    The instructions linked above also show how to hot reload things outside of the component tree, such as redux reducers.

    0 讨论(0)
提交回复
热议问题