未找到模块: “ redux”

我已经使用 create-react-app cli 创建了一个新的反应应用程序。 然后我使用 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"
}
}
118284 次浏览

You need to install react-redux but also redux library.

npm install --save redux

react-redux internally uses Action, ActionCreator, AnyAction, Dispatch, Store these interfaces form redux package.

the moment you call

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

react-redux try to make use of all these interfaces from redux package. which is not present at the moment.

So you may have to install react-redux package along with redux since both have dependency.

 npm install --save redux react-redux

I had the same challenge while working with Visual Studio Code (VSC) IDE.

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

The 'redux' package was being resolved from another place all together, as a dependency within some typescript packages.

I ran yarn add redux@3.7.2 (with the VSC terminal) to re-install the package. Note that i had the exact version within my package.json as a dependency, and this helps yarn to link the dependencies faster since my goal was not to upgrade redux yet.

Set moduleResolution to node at tsconfig.json

Example:

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

You can use the following command:

using npm

npm install redux --save

using yarn

yarn add redux

I put my all redux related files to src/redux folder and after some search, I found that this folder name can cause conflicts. After changing the folder name everything works fine.

using npm :

npm install redux --save

using yarn :

yarn add redux

Just install react-redux if it is not present in package.json using

npm i react-redux

or install redux if it is not there in package.json

npm i redux

You need to install react-redux but also a redux library, both are kind of dependent on each other. Don't forget to import it.

npm install --save redux

For me in typescript it worked as follows:

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

(or with npm try: npm install add @types/redux @types/react-redux)