Module not found when import .jsx file

Rido picture Rido · Aug 29, 2017 · Viewed 9.2k times · Source

I can't find out the solution. I'm using Reactstrap (CSS framework), React, Express, and Webpack.

I was success to import App.jsx file on index.jsx. Then, I tried to import NavbarTemplate.jsx file on App.jsx by using the same way. But, it displayed error like this :

ERROR in ./client/src/components/App.jsx Module not found: Error: Can't resolve 'NavbarTemplate.jsx' in '/Users/oprent1/v2/oprent-react/client/src/components' @ ./client/src/components/App.jsx 11:22-51 @ ./client/src/index.jsx

What is wrong with my configuration ? I have provided several files that related to this below :

webpack.config.js

const path = require('path');

module.exports = {
  entry: path.join(__dirname, '/client/src/index.jsx'),
  output: {
    path: path.join(__dirname, '/client/dist/js'),
    filename: 'app.js',
  },

  module: {
    loaders: [
      {
        test: /\.jsx?$/,
        include: path.join(__dirname, '/client/src'),
        // loader: 'babel',
        loader: 'babel-loader',
        query: {
          presets: ["react", "es2015"],
          plugins: ["transform-class-properties"]
        }
      },
      {
        test: /\.css$/,
        loader: 'style-loader!css-loader'
      }
    ],
  },
  watch: true
};

index.jsx

import React from 'react';
import ReactDOM from 'react-dom';
import Bootstrap from 'bootstrap/dist/css/bootstrap.css';
import App from './components/App.jsx'; //SUCCESS when imported 

ReactDOM.render(
  <App />,
  document.getElementById('react-app')
);

App.jsx

import React from 'react';
import NavbarTemplate from 'NavbarTemplate.jsx'; //ERROR when imported

const App = (props) => {
  return (
    <div>
        <NavbarTemplate />
    </div>
  );
};

export default App;

NavbarTemplate.jsx

import React, { PropTypes } from 'react';
import { Collapse, Navbar, NavbarToggler, NavbarBrand, Nav, NavItem, NavLink } from 'reactstrap';

class NavbarTemplate extends React.Component {

  constructor(props) {
    super(props);

    this.toggleNavbar = this.toggleNavbar.bind(this);
    this.state = {
      collapsed: true
    };
  }

  toggleNavbar() {
    this.setState({
      collapsed: !this.state.collapsed
    });
  }

  render() {
    return (
      <div>
        <Navbar color="faded" light>
          <NavbarToggler onClick={this.toggleNavbar} />
          <Collapse className="navbar-toggleable-md" isOpen={!this.state.collapsed}>
            <NavbarBrand href="/">reactstrap</NavbarBrand>
            <Nav navbar>
              <NavItem>
                <NavLink href="/components/">Components</NavLink>
              </NavItem>
              <NavItem>
                <NavLink href="https://github.com/reactstrap/reactstrap">Github</NavLink>
              </NavItem>
            </Nav>
          </Collapse>
        </Navbar>
      </div>
    );
  }
}

export default NavbarTemplate;

Folder Structure

enter image description here

Answer

Miguel picture Miguel · Aug 29, 2017

To import files in the same folder you have to use

./nameOfTheFile

Instead of

nameOfTheFile

So your important statement would be

import NavbarTemplate from './NavbarTemplate.jsx';