简体   繁体   English

React 导入问题,“找不到模块:无法解析”

[英]React import problem, 'Module not found: Can't resolve'

I have a folder system in my React project, which you can see in the screenshot below:我的 React 项目中有一个文件夹系统,您可以在下面的屏幕截图中看到:

在此处输入图像描述

I am trying to make import to index.js , located in src/components/KeyboardCard/index.js , from file context.js that is located in src/context.js .我正在尝试从位于src/context.js的文件context.js导入位于src/components/KeyboardCard/index.js index.js index.js 。

在此处输入图像描述

I am getting this error message:我收到此错误消息:

Module not found: Can't resolve '.../context.js' in 'C:\React\top-board\src\components\KeyboardCard'.找不到模块:无法解析“C:\React\top-board\src\components\KeyboardCard”中的“.../context.js”。

Help me please.请帮帮我。

You can't have triple dots import there, try:你不能在那里导入三重点,试试:

import AppContext from '../../context.js';

It's two levels higher:它高了两个级别:

import AppContext from '../../context';

You have provided the wrong path.您提供了错误的路径。

import AppContext from '../../context.js';

If you want to go 2 levels higher you have to do it like this:如果你想 go 高 2 级,你必须这样做:

../../context

Also, you don't have to define file format ".js" there, that's not required.此外,您不必在那里定义文件格式“.js”,这不是必需的。

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

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