29

I just started with React.js and I am unable to import component.

I have this structure as followed by this tutorial (YouTube link) :

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

This is my 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'));

This is MyComponent.js:

import React from "react";

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

I am using this webpack file (GitHub link).

However, when I run this, my module fails to load.

I get this error in the browser console:

Error: Cannot find module "./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

Can't figure out what went wrong here.

8 Answers 8

53

For anyone coming here without a typo, and is using Webpack, be sure to check for a clause like this:

resolve: {
    extensions: [".jsx", ".js"]
},

in your webpack.config.js.

This tells your transpiler to resolve statements like:

import Setup from './components/Setup'

to

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

This way you don't need the extension.

Sign up to request clarification or add additional context in comments.

2 Comments

this helped . I missed a dot in the extension type like extensions: ["jsx", ".js"] instead of extensions: [".jsx", ".js"]
After doing this I had to fully rerun webpack for the changes to apply, --watch did not catch them.
29

You have a typo in your import. You're requesting MyCompontent. Change to:

import MyComponent from "./components/MyComponent";

And all typos as well.

4 Comments

You have a typo, might be relevant: MyCompontent instead of MyComponent
Yes that was a typo. I am sorry.
Also make sure to npm start after the typo correction if the problem exists.
This may or may not be relevant for this situation but also be mindful of case in your directory paths. I had a situation where my local directories were lower case, but in prod they were upper. I had to completely wipe away my local structure and clone from prod to fix it, otherwise I was going to spend a lot of time refactoring directory names.
4

You can try to import MyCompontent from "./components/MyCompontent.js"

like this

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

Comments

2

I just had this issue, no type or webpack config issues.

What fixed it was changing the import from relative to the app root directory to relative to the file:

import MyComponent from "src/components/MyComponent";

to

import MyComponent from "../components/MyComponent";

If you're getting this from Visual Studio Code auto-importing via the shortest route, you can change it so it imports relatively. By going here:

menu File → Preferences → Settings → User Settings,

"typescript.preferences.importModuleSpecifier": "relative"

Comments

1

You have written that the filename is MyComponent.js.

Thus, your import should look like

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

Comments

1

The problem for me was that import line was not generated correctly. I have this scenario:

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

this is myCustomText.tsx file:

export interface Props {
  text: string;
}

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

export default MyCustomText

And the generated import was this:

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

and I changed it to this:

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

I don't know why the generated import line was generated automatically wrong.

Comments

1

I found myself here without a typo and without a webpack issue.

The solution for me was to restart the typescript server via VS Code.

Comments

1

export 'Component' (imported as 'Component') was not found in 'react'

if you find your self stuck with this error simply go to mini-create-react-context, and go to cjs, and go to index.js and add "React" example: you will find this (Component) solution (React.Component) only if you extends to React.Component in you pages

Note: I have only used this on VS Code

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.