简体   繁体   English

反应编译错误,找不到模块

[英]react compile error, Module not found

I got this error in my browser 我的浏览器出现此错误

Error in ./src/App.js
Module not found: ./components/todo in C:\Users\James\Desktop\react\src

This is what I got in my editor 这就是我在编辑器中得到的

import {TodoForm, TodoList} from './components/todo'

http://imgur.com/a/8YLod http://imgur.com/a/8YLod

I even tried import {TodoForm, TodoList} from './components/todo/' I wonder what's wrong. 我什至尝试import {TodoForm, TodoList} from './components/todo/'我不知道怎么了。

Imports work on a per module basis for most loaders/bundlers. 对于大多数装载机/捆扎机,每个模块的导入工作都是如此。 In other words, you'll need to import the form and list by doing the following: 换句话说,您需要通过执行以下操作来导入表单和列表:

import { TodoForm } from './components/todo/TodoForm'
import { TodoList } from './components/todo/TodoList'

As a side note, see https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export to make sure that you're exporting the components correctly. 作为附带说明,请参阅https://developer.mozilla.org/en/docs/web/javascript/reference/statements/export ,以确保您正确导出了组件。 The curly braces around your import works for a named export as opposed to a default export. 导入周围的花括号适用于命名导出,而不是默认导出。

So the thing is that when you do 所以事情是当你做

import {TodoForm, TodoList} from './components/todo'

What happends is that your compiler will by default search the components TodoForm and TodoList from the index.js file since you have not mentioned explicitly which files to point to 发生的情况是,默认情况下,编译器将从index.js文件中搜索组件TodoFormTodoList ,因为您没有明确提及要指向的文件

So if in index.js you add something like 因此,如果在index.js中添加类似

export * from './components/todo/TodoForm';
export * from './components/todo/TodoList';

Your approach will work. 您的方法会起作用。

In order to just import all files from the directory you must have an index.js file that exports everything from the directory 为了只导入目录中的所有文件,您必须具有一个index.js文件,该文件可以导出目录中的所有内容

Which in your case the index.js file would look like this 在您的情况下,index.js文件应如下所示

Export * from 'TodoForm'
Export * from 'TodoList'

Note if the export statement doesn't work see this answer to fix the import / export statement 请注意,如果导出语句不起作用,请参见以下答案以修复导入/导出语句

Do you think, when you wrote import {TodoForm, TodoList} from './components/todo' , in TodoForm should be value than you exported from file TodoForm.js , and similarly with TodoList ? 您是否认为,当您import {TodoForm, TodoList} from './components/todo'编写import {TodoForm, TodoList} from './components/todo' ,在TodoForm应该比从文件TodoForm.js导出的值更TodoForm.js ,并且与TodoList类似? It's don't works. 这是行不通的。 You should do import from some file. 您应该从某些文件导入。 When you wrote from './components/todo' you tried doing import from todo directory. 当您from './components/todo'编写时from './components/todo'您尝试从todo目录导入。 I guess, in egghead video that import works, because, they have index.js file in todo directory. 我猜,在导入的蛋壳视频中,因为它们在todo目录中具有index.js文件。 And in this file they do export for every component separately. 并且在此文件中,它们确实分别为每个组件导出。 Try to add index.js file in todo directory with the following contents: 尝试将以下内容添加到todo目录中的index.js文件:

export * from './TodoForm.js';
export * from './TodoList.js';

it's will work. 它会工作的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM