1

All the files inside src directory(using create-react-app). I can't seem to import search.js component into App.js file

src/App.js file code -

import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import SearchProfile from './search';

    class App extends Component {
      render() {
        return (
          <div className="App">
            <header className="App-header">
              <img src={logo} className="App-logo" alt="logo" />
              <h1 className="App-title">Welcome to React</h1>
            </header>
            <p className="App-intro">
              To get started, edit <code>src/App.js</code> and save to reload.
            </p>
          </div>
        );
      }
    }

    export default App;

src/search.js file code -

import React, { Component } from 'react';

export default class SearchProfile extends React.Component {
  render() {
    return (
      <div className="search--box">
         <form onSubmit={this.handleForm.bind(this)}>
           <label><input type="search" ref="username" placeholder="Type Username + Enter"/></label>
         </form>
      </div>
    )
  }

  handleForm(e) {
   e.preventDefault();
    let username = this.refs.username.getDOMNode().value
    this.props.fetchProfile(username);
    this.refs.username.getDOMNode().value = '';
  }
}

I'm getting this error -

Failed to compile ./src/App.js
Module not found: Can't resolve './src/search'

I can't seem to find why it's not importing the module.

src/index.js file code -

import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import registerServiceWorker from './registerServiceWorker';

ReactDOM.render(<App />, document.getElementById('root'));
registerServiceWorker();
3
  • If both file are in the same folder just use import SearchProfile from './search'; Commented Jul 29, 2018 at 12:21
  • please check the code, I did but it's not working Commented Jul 29, 2018 at 12:33
  • Check your file name for search Commented Jul 29, 2018 at 13:51

2 Answers 2

5

Your search.js has a couple of errors. You are not importing React as you do in app.js, you are exporting same class twice and you are importing App component. Try removing first and last line of file and import React, similar to how you do it App.js

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

3 Comments

Removed first and last line, imported react just like app.js still getting error
Try restarting build and make sure search.js is in same folder as App.js. I copied your code and it's fine: stackblitz.com/edit/react-xsd6td?file=src%2Fsearch.js
I had file name - "search,js" instead of "search.js" . Thank you for the help. The height of my stupidity :'(
1

Here's an MWE for your app.

There were a couple of errors:

  • no import for React in your App component file
  • you probably forgot to add the file name when importing SearchProfile (assuming it's in a directory "search" without "index.js" file)
  • double default export for SearchProfile

I marked those in the code.

3 Comments

Edited the question, Please check now. All the files ( app.js / index.js / search.js ) in the src directory
That works too: link.
I had file name - "search,js" instead of "search.js" . Thank you for the help. The height of my stupidity :'(

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.