Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why do i get the Error Module not found: Can't resolve 'redux'

I'm new to redux I have created a demo wherein I have install react redux and redux

Folder Structure

  src
    js
      store
        index.js
      action
        index.js
      reducers
        index.js
      index.js  ///////**2**
  index.js ///////////  **1**
  App.js .... etc

In 1 index.js I have the following code

import index from "./js/index";

In 2 index.js I have the following code

   import store from "../js/store/index";
   import { addArticle } from "../js/actions/index";

   window.store=store;
   window.addArticle=addArticle;

packages are all updated when I npm start I get the following error;

   ./src/js/store/index.js
   Module not found: Can't resolve 'redux' in 'E:\reacr-redux\src\js\store'

js/store/index.js

     import { createStore } from "redux";
     import rootReducer from "../reducers/index";

     const store=createStore(rootReducer);

     export default store;

reducers/index.js

import { ADD_ARTICLE } from "../constants/action-types";
 const initialState={
articles:[]
 };
  const rootReducer= ( state = initialState ,  action ) => {
    switch(action.type){
    case ADD_ARTICLE:
    state.articles.push(action.payload);
    return state;
    default :
    return state;
    }
   };
    export default rootReducer;

package.json

{
"name": "reacr-redux",
"version": "0.1.0",
 "private": true,
 "dependencies": {
"font-awesome": "^4.7.0",
"mdbootstrap": "^4.5.15",
"react": "^16.6.3",
"react-dom": "^16.6.3",
"react-redux": "^5.1.1",
"react-router-dom": "^4.3.1",
"react-scripts": "2.1.1"
 },
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"test": "react-scripts test",
"eject": "react-scripts eject",

 },
 "eslintConfig": {
   "extends": "react-app"
  },
 "browserslist": [
  ">0.2%",
  "not dead",
  "not ie <= 11",
  "not op_mini all"
  ],
 "devDependencies": {
   "redux": "^4.0.1"
   }
  }

Can anyone lemme know whats happening I'm new to it where m I going wrong

like image 608
Tested Avatar asked Dec 01 '18 09:12

Tested


People also ask

How do you handle errors in Redux?

Usually the best approach to error handling with redux is to have an error field in state that is then passed to an error component. The error component doesn't have to just display an error, it could also do side effects with useEffect . How the error is set/unset depends on your application.

What can I use instead of Redux?

The simplest alternative to Redux is to use React's built in Contexts functionality. One of the many important pieces of functionality state management libraries provide is the ability to access content across multiple distant components.


3 Answers

Do not use save-dev for Redux. Remove it from devDependencies:

npm uninstall redux --save-dev

and install it again with:

npm i redux --save
like image 183
Jihlavanka Avatar answered Sep 28 '22 09:09

Jihlavanka


Simply installing the react toolkit should fix the problem.

# NPM
npm install @reduxjs/toolkit

# Yarn
yarn add @reduxjs/toolkit
like image 37
Azaria Gebremichael Avatar answered Sep 28 '22 10:09

Azaria Gebremichael


after running npm install @reduxjs/toolkit try restarting your server.

like image 45
Macadadi Avatar answered Sep 28 '22 09:09

Macadadi