React无法导入组件 - 找不到模块

时间:2016-12-25 16:44:45

标签: javascript reactjs webpack

我刚开始使用React.js,我无法导入组件。

我有这个结构,后跟this tutorial (YouTube link)

-- src
----| index.html
----| app
------| index.js
------| components
--------| MyCompontent.js

这是我的index.js

import React from 'react';
import { render } from 'react-dom';

import { MyCompontent } from "./components/MyCompontent";

class App extends React.Component {
    render() {
        return (
            <div>
              <h1>Foo</h1>
              <MyCompontent/>
            </div>
        );
    }
}

render(<App />, window.document.getElementById('app'));

这是MyComponent.js

import React from "react";

export class MyCompontent extends React.Component {
  render(){
    return(
      <div>MyCompontent</div>
    );
  }
}

我正在使用this webpack file (GitHub link)

然而,当我运行它时,我的模块无法加载。

我在浏览器控制台中收到此错误:

  

错误:找不到模块“./components/MyCompontent”

[WDS] Hot Module Replacement enabled.  bundle.js:631:11
[WDS] Errors while compiling.  bundle.js:631:11
./src/app/index.js
Module not found: Error: Cannot resolve 'file' or 'directory' ./components/MyCompontent in /home/kuno/code/react-hoteli/src/app
resolve file
  /home/kuno/code/react-hoteli/src/app/components/MyCompontent doesn't exist
  /home/kuno/code/react-hoteli/src/app/components/MyCompontent.webpack.js doesn't exist
  /home/kuno/code/react-hoteli/src/app/components/MyCompontent.web.js doesn't exist
  /home/kuno/code/react-hoteli/src/app/components/MyCompontent.js doesn't exist
  /home/kuno/code/react-hoteli/src/app/components/MyCompontent.json doesn't exist
resolve directory
  /home/kuno/code/react-hoteli/src/app/components/MyCompontent/package.json doesn't exist (directory description file)
  /home/kuno/code/react-hoteli/src/app/components/MyCompontent doesn't exist (directory default file)
[/home/kuno/code/react-hoteli/src/app/components/MyCompontent]
[/home/kuno/code/react-hoteli/src/app/components/MyCompontent.webpack.js]
[/home/kuno/code/react-hoteli/src/app/components/MyCompontent.web.js]
[/home/kuno/code/react-hoteli/src/app/components/MyCompontent.js]
[/home/kuno/code/react-hoteli/src/app/components/MyCompontent.json]
 @ ./src/app/index.js 11:20-56  bundle.js:669:5

无法弄清楚这里出了什么问题。

5 个答案:

答案 0 :(得分:29)

对于来这里的人 没有 一个拼写错误,并且正在使用Webpack,请务必检查这样的条款:

resolve: {
    extensions: [".jsx", ".js"]
},
webpack.config.js 中的

这告诉您的转换器解析如下语句:

import Setup from './components/Setup'

import Setup from './components/Setup.jsx'

这样您就不需要扩展程序了。

答案 1 :(得分:14)

导入时输入错误。您正在请求MyCompontent。改为:

import MyComponent from "./components/MyComponent";

所有拼写错误。

答案 2 :(得分:2)

您可以尝试从“./components/MyCompontent.js”导入MyCompontent

像这样

import MyCompontent from "./components/MyCompontent.js";

答案 3 :(得分:0)

您已写过文件名为MyComponent.js。

因此,您的导入应该看起来像

import { MyCompontent } from './components/MyComponent.js'

答案 4 :(得分:0)

对我来说,问题是导入行未正确生成。我有这种情况:

--src
----elements
-----myCustomText.tsx

这是myCustomText.tsx文件:

export interface Props {
  text: string;
}

const MyCustomText = ({ text }: Props) => (
  <Text>{text}</Text>
);

export default MyCustomText

生成的导入是这样的:

import MyCustomText from '../../elements/MyCustomText';

然后我将其更改为:

import MyCustomText from '../../elements/myCustomText'

我不知道为什么生成的导入行自动生成错误。