2016-08-20 93 views
1

首先,我是React with Rails的初学者并尝试了一些事情。我目前正在使用路由,并且我不确定哪里出错。请指导我。未被捕获的错误:找不到模块“反应”

我有一个UserWorld.jsx文件的容器文件夹。其内容如下:

import React, { PropTypes } from 'react'; 
import User from '../containers/User'; 

// Simple example of a React "smart" component 
export default class UserWorld extends React.Component { 
    constructor(props, context) { 
    super(props, context); 
    // How to set initial state in ES6 class syntax 
    // https://facebook.github.io/react/docs/reusable-components.html#es6-classes 
    this.state = { users: this.props.data 
    }; 
    } 
    render() { 
    var userNodes = this.state.users.map(function(user) { 
     return (
      <User key={user.id} first_name={user.first_name} last_name={user.last_name} email={user.email} city={user.city} address={user.address} /> 
    ); 
    }); 
    return (
     <table> 
     <thead> 
      <tr> 
      <td>First Name</td> 
      <td>Last Name</td> 
      <td>Email</td> 
      <td>Address</td> 
      <td>City</td> 
      </tr> 
     </thead> 
     <tbody> 
      {userNodes} 
     </tbody> 
     </table> 
    ); 
    } 
} 

现在,我想添加一个链接,我包括

import { Router, Route, Link, browserHistory, IndexRoute } from 'react-router'

但是这给了我一个错误在控制台,如下所示:

Uncaught Error: Cannot find module "react" 
Exception in rendering! 
message: Could not find component registered with name UserWorldApp. Registered component names include [ HelloWorldApp ]. Maybe you forgot to register the component? 

现在我不知道这里发生了什么。如果有人能帮助我在这里,我将不胜感激。另外,如果我可以得到相同的参考。我遇到了各种各样的语法问题。

更新:

我看到错误出现在var _react = require('react');线在Router.js在反应路由器文件夹中的文件夹node_modules。

我得到这个控制台:

ERROR in ../~/react-router/lib/Router.js 
18:03:09 client.1 | Module not found: Error: Cannot resolve module 'imports' in /Users/c193/Documents/React/test-react-on-rails/node_modules/react-router/lib 
18:03:09 client.1 | @ ../~/react-router/lib/Router.js 19:13-29 

P.S:所有的文档和文章我读实在是太技术,我听不懂,我只了解位和它的一部分。刚开始我的两周时间。

+0

你安装'react'?你有'package.json'文件吗? – JMM

+0

是的,我有'包。json'文件和我安装了反应和反应路由器使用'npm我反应react-dom react-router -save'和webpack使用'npm我webpack webpack-dev-server babel-core babel-loader babel-preset-es2015 babel -preset-react --save-dev' – Aakanksha

+0

很难说出发生了什么,因为你有太多的错误消息。例如,您发布了此错误消息:'错误:无法解析模块'imports''。目前还不清楚这与'无法找到模块'反应''错误有什么关系。 React和React Router在同一个'node_modules /'中,对吗? – JMM

回答

0

我不知道问题是什么或我错过了什么。但是,在一个全新的项目中,我跑了npm install react-router --save。我把import { Link } from 'react-router/lib/Link';放在我的反应组件中。重新启动服务器,我很高兴去。我在我以前的应用程序中执行了相同的步骤,但它在某种程度上不适合我。无论如何,我不知道我错过了什么,但问题现在解决了。

P.S:我现在正在面对一个新问题,并就此提出一个新问题。

谢谢反正人。 :)

0

我有类似的问题,但与“反应路由器”,而不是“反应”。检查client目录中的package.json,如果缺少任何内容,请手动添加它并从该目录运行npm install。这解决了我的问题,虽然有人评论说,你也可能在这里还有其他一些问题。

我不认为从根项目目录运行npm install react-router --save是一个好主意,因为on Rails的阵营使用的纱线,所以在主项目文件夹的目录node_modules唯一应该是一个.yarn.integrity文件。 React所需的所有内容应位于client文件夹中的package.json文件和node_modules目录中,因此请检查是否有缺少的内容。

这里是他们的client/package.json看起来像在教程:https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/client/package.json

相关问题