未捕获的错误:找不到模块“react" [英] Uncaught Error: Cannot find module "react"

查看:45
本文介绍了未捕获的错误:找不到模块“react"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是 React 和 Rails 的初学者,并尝试了一些东西.我目前正在处理路由,我不确定我哪里出错了.请指导我.

starting with, i am a beginner in React with Rails and trying things out. I am currently working with routing and i'm not sure where i'm going wrong. Please guide me.

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

I have a container folder with UserWorld.jsx file. Its content are as follows:

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>
    );
  }
}

现在,我想添加一个链接,我要添加

Now, i wish to add a link and i'm including

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

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

However this gives me an error in the console as follows:

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?

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

Now i have no clue about whats happening here. I would be obliged if someone could help me out here. Also, if i can get references for the same. I am facing trouble with the different syntax that are found all around.

更新:

我看到错误出现在 node_modules 文件夹中 react-router 文件夹中 Router.js 的 var _react = require('react'); 行中.

I see that the error is appearing in the var _react = require('react'); line in the Router.js in react-router folder in the node_modules folder.

我在控制台中得到这个:

I get this in console:

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:我阅读的所有文档和文章都太技术性了,我无法理解,我只能理解其中的一点点.从我开始到现在才两周.

P.S: All the docs and articles i read are way too technical for me to understand and i only understand bits and pieces of it. Its just been two weeks since i started.

推荐答案

我遇到了类似的问题,但使用的是react-router"而不是react".检查 client 目录中的 package.json,如果缺少任何内容,请手动添加并从该目录运行 npm install.这为我解决了这个问题,尽管正如有人评论的那样,您可能还有其他一些问题.

I had a similar issue, but with "react-router" instead of "react." Check the package.json in the client directory, and if anything's missing, add it manually and run npm install from that directory. That solved the issue for me, although as somebody commented, you probably have a few other issues here as well.

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

I don't think running npm install react-router --save from the root project directory is a good idea, because React on Rails uses yarn, so the only thing in the node_modules directory in the main project folder should be a .yarn.integrity file. Everything you need for React should be in the package.json file and node_modules directory in the client folder, so check if something's missing from there.

这是他们的 client/package.json 在教程中的样子:https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/client/package.json

Here's what their client/package.json looks like in the tutorial: https://github.com/shakacode/react-webpack-rails-tutorial/blob/master/client/package.json

这篇关于未捕获的错误:找不到模块“react"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆