2016-08-20 86 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

相關問題