找不到模块:无法解决

时间:2018-04-20 14:19:21

标签: javascript reactjs

我正在使用React.js开发一个组件,而控制台正在抛出以下错误

无法编译:

./src/components/file_tree.js
   Module not found: Can't resolve '.src/components/directory.js' in 
'D:\Treebeard\src'

您可以在此处查看 file_tree.js

中的代码
import React from 'react';
import ReactDOM from 'react';
import { Directory } from './components/directory.js';

directory.js 中的代码:

import React from 'react';
import { TriangleDown } from './components/file_tree.js';
import { FolderIcon } from './components/file_tree.js';
import { formatName } from './components/file_tree.js';
import { renderTree } from './components/file_tree.js';

这里是 App.js 代码,以防万一:

import React from 'react';
import ReactDOM from 'react-dom';
import { SearchEngine } from './components/search_engine.js';
import { FileTree } from './components/file_tree.js';
import { Directory } from './components/directory.js';

我检查了两个文件中的文件路径,一切似乎没问题,但是抛出了这个错误。我错过了一些关于文件路径的内容吗? directory.js file_tree.js 都在' ./ src / components' 文件夹中。

App.js 位于' ./ src' 文件夹中。

1 个答案:

答案 0 :(得分:1)

因为您正在从同一目录访问文件路径。它应该是:

<强> file_tree.js:

import React from 'react';
import ReactDOM from 'react';
import { Directory } from './directory.js';

<强> directory.js:

import React from 'react';
import { TriangleDown } from './file_tree.js';
import { FolderIcon } from './file_tree.js';
import { formatName } from './file_tree.js';
import { renderTree } from './file_tree.js';

App.js:没关系。由于您正在从当前目录访问组件文件路径。

import React from 'react';
import ReactDOM from 'react-dom';
import { SearchEngine } from './components/search_engine.js';
import { FileTree } from './components/file_tree.js';
import { Directory } from './components/directory.js';

如需进一步的帮助,请参阅此post,这将在相关案例中为您提供帮助。