React无法从目录/ index.js导出文件,而是查找directory.js

时间:2017-11-03 06:01:24

标签: reactjs react-native

我正在开发基本的反应应用并遵循此github react respository

以下是项目目录中的文件夹结构( / opt / rqt /

src/
├── App.css
├── actions.js
├── components
│   ├── App.js
│   ├── Footer.js
│   ├── Link.js
│   ├── Todo.js
│   └── TodoList.js
├── containers
│   ├── AddTodo.js
│   ├── FilterLink.js
│   └── VisibleTodoList.js
├── index.js
├── logo.svg
├── reducers
│   ├── index.js
│   ├── todos.js
│   └── visibilityFilter.js
└── registerServiceWorker.js

/src/index.js

import React from 'react'
import ReactDOM from 'react-dom'
import { createStore } from 'redux'
import { Provider } from 'react-redux'
import App from './components/App'
import reducer from './reducers'
// import todoApp from './reducers' // trial 2
import './App.css'
import registerServiceWorker from './registerServiceWorker';

registerServiceWorker();

const store = createStore(reducer);
// const store = createStore(todoApp); // trial 2

ReactDOM.render(
  <Provider store={store}>
    <App />
  </Provider>,
  document.getElementById('root')
);

/src/reducers/index.js

import { combineReducers } from 'redux'
import todos from './todos'
import visibilityFilter from './visibilityFilter'
const todoApp = combineReducers({
    todos,
    visibilityFilter
});

export default todoApp

但是当运行npm start时,它会在浏览器中显示以下错误

  

无法编译./src/reducers.js模块构建失败:错误:   ENOENT:没有这样的文件或目录,打开&#39; /opt/rqt/src/reducers.js'       在错误(本机)

为什么只反应找到文件名 reducers.js 而不是寻找 reducers / index.js

还试图附加./reducers/但不能正常工作

这里有什么问题?

1 个答案:

答案 0 :(得分:2)

使用React启动时,我遇到了类似的问题。显然,这是由React DOM中的错误引起的。有一个类似的Github issue here

我所做的是终止该过程并重新安装依赖项npm install

然后,它与代码一起工作:

/src/reducers/index.js

export default combineReducers({
    // Some Code Here
})

然后,您可以轻松地将其导入 App.js index.js

import rootReducer from "../reducers"

因此,您可以将rootReducer替换为您喜欢的任何名称。