5

I created a new react project using Create-React-App. In the old projects, Whenever I did the changes in the component and save the component it will reflect in the browser, but in the new project when I saved the changes in the code the browser did not reload and not reflect the changes. So I stopped the running process and again giving the npm start

Here is my package.json file.

{
  "name": "new-application",
  "version": "0.1.0",
  "private": true,
  "dependencies": {
    "@testing-library/jest-dom": "^4.2.4",
    "@testing-library/react": "^9.4.0",
    "@testing-library/user-event": "^7.2.1",
    "react": "^16.12.0",
    "react-dom": "^16.12.0",
    "react-redux": "^7.1.3",
    "react-router-dom": "^5.1.2",
    "react-scripts": "3.3.0",
    "redux": "^4.0.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test",
    "eject": "react-scripts eject"
  },
  "eslintConfig": {
    "extends": "react-app"
  },
  "browserslist": {
    "production": [
      ">0.2%",
      "not dead",
      "not op_mini all"
    ],
    "development": [
      "last 1 chrome version",
      "last 1 firefox version",
      "last 1 safari version"
    ]
  },
  "devDependencies": {
    "redux-devtools": "^3.5.0",
    "webpack": "^4.41.5"
  }
}

Can I add something in package.json to reload the browser automatically whenever I done changes?

6
  • What you want is called "hot module reloading". There are packages for it, such as this one: github.com/gaearon/react-hot-loader Commented Feb 10, 2020 at 7:39
  • in the terminal is the script running after making changes to any component and saving them? Commented Feb 10, 2020 at 7:39
  • I was under the impression that create-react-app has hot module reloading enabled by default. Commented Feb 10, 2020 at 7:42
  • @Ramesh no bro in the terminal itself script is not running. Commented Feb 10, 2020 at 7:42
  • Yes @JMadelaine but it's not working. Commented Feb 10, 2020 at 7:43

3 Answers 3

1

I had a similar (or event the same) problem and I changed starting command in the package.json file by adding following flags: --watch --watch-poll to the webpack-dev-server:

{
//...
"scripts": {
    "start": "webpack-dev-server --env.ENVIRONMENT=development --content-base src/ --mode development --watch --watch-poll",
    // ...
}
// ...
}

Now, using npm start and then changing src files I can see changes in the browser.

Please here https://webpack.js.org/configuration/watch/ for more options.

Sign up to request clarification or add additional context in comments.

Comments

0

Install your app again using :

npx create-react-app appname

And don't edit package.json

Comments

0

If you prefer not modifying the package.json you can reinstall your project.

Two ways for that.

With git:

  1. Push your project on git
  2. Clear the project's folder on your computer
  3. git clone https://github.com/my-account/my-project.git
  4. npm install

Without git:

  1. Back up the content of your project except for node_modules
  2. Clear the project's folder except for package.json
  3. npm install
  4. Paste the files you backed up

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.