找不到模块:'redux'

Module not found: 'redux'

我已经使用 create-react-app cli 创建了一个新的 React 应用程序。 然后我使用 npm install --save react-redux.

添加了 'react-redux'

package.json我有:

"react-redux": "^4.4.5"

不幸的是,该应用程序无法编译并抱怨:

Error in ./~/react-redux/lib/utils/wrapActionCreators.js
Module not found: 'redux' in C:\Users\Salman\Desktop\Courses\Internship\React\Youtube-Front-End\node_modules\react-redux\lib\utils

 @ ./~/react-redux/lib/utils/wrapActionCreators.js 6:13-29

我不知道这是什么意思?

这是完整的容器:

import React,{Component} from 'react';
import {connect} from 'react-redux';

class BookList extends Component{
  renderList(){
        return this.props.books.map((book)=>{
          <li key={book.title} >{book.title}</li>
        });
    }

  render(){

    return(
      <div>
        <ul>
          {this.renderList()}
        </ul>
      </div>
    );
  }
}

function mapStateToProps(state){
  return{
    books:state.books
  };
}

export default connect(mapStateToProps)(BookList);

这里是完整的package.json:

{
  "name": "Youtube-Front-End",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "react-scripts": "0.6.1",
    "webpack": "^1.13.2"
  },
  "dependencies": {
    "react": "^15.3.2",
    "react-dom": "^15.3.2",
    "react-redux": "^4.4.5",
    "youtube-api-search": "0.0.5"
  },
  "scripts": {
    "start": "react-scripts start",
    "build": "react-scripts build",
    "test": "react-scripts test --env=jsdom",
    "eject": "react-scripts eject"
  }
}

你需要安装 react-redux 和 redux 库。

npm install --save redux

react-redux 内部使用 Action, ActionCreator, AnyAction, Dispatch, Store 这些接口形成 redux 包。

你打电话的那一刻

export default connect(mapStateToProps,mapDispatchToProps)(App);

react-redux 尝试使用 redux 包中的所有这些接口。目前不存在。

因此您可能需要安装 react-redux 软件包以及 redux,因为两者都有依赖性。

 npm install --save redux react-redux

我在使用 Visual Studio 代码 (VSC) IDE 时遇到了同样的挑战。

import { createStore, combineReducers, applyMiddleware } from 'redux';

'redux' 包正在从另一个地方一起解析,作为某些 typescript 包中的依赖项。

我运行yarn add redux@3.7.2(用VSC终端)重新安装包。请注意,我在 package.json 中有确切的版本作为依赖项,这有助于 yarn 更快地 link 依赖项,因为我的目标还不是升级 redux。

moduleResolution 设置为 node at tsconfig.json

示例:

  "compilerOptions": {
    "moduleResolution": "node"
  }

您可以使用以下命令:

使用 npm

npm install redux --save

使用纱线

yarn add redux

我把我所有的 redux 相关文件都放在 src/redux 文件夹中,经过一番搜索,我发现这个文件夹名称会导致冲突。更改文件夹名称后一切正常。

使用 npm :

npm 安装 redux --save

使用纱线:

纱线添加 redux

如果 package.json 中没有 react-redux,只需安装它 使用

npm i react-redux

如果 package.json

中没有 redux,则安装它

npm i redux

你需要安装 react-redux 和 redux 库,两者是相互依赖的。不要忘记导入它。

npm install --save redux

对我来说,打字稿的工作方式如下:

yarn add @types/redux @types/react-redux

(或使用 npm 尝试:npm install add @types/redux @types/react-redux