为什么我会收到错误模块未找到:无法解析“redux”

问题描述 投票:0回答:4

我是 Redux 新手,我创建了一个演示,其中安装了 React Redux 和 Redux。

文件夹结构:

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

1index.js中我有以下代码:

import index from "./js/index";

2index.js 我有以下代码:

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

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

套餐已全部更新。
当我这样做时

npm start
我收到以下错误:

./src/js/store/index.js
找不到模块:无法解析“E”中的“redux”: eacr-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"
   }
  }

谁能告诉我发生了什么事吗?
我是新手,我哪里出错了?

reactjs redux react-redux
4个回答
14
投票

请勿将

save-dev
用于 Redux。将其从
devDependencies
中删除:

npm uninstall redux --save-dev

然后再次安装:

npm i redux --save

14
投票

只需安装 react 工具包 就可以解决问题。

# NPM
npm install @reduxjs/toolkit

# Yarn
yarn add @reduxjs/toolkit

2
投票

运行后 npm install @reduxjs/toolkit 尝试重新启动服务器。


-1
投票

用它来安装 React Redux npm install @reduxjs/toolkit

© www.soinside.com 2019 - 2024. All rights reserved.