Module not found when import .jsx file

20.4k views Asked by At

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

5

There are 5 answers

1
Miguel On BEST ANSWER

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';
0
Vikram Saini On

so basically . represents the parent folder when you try to do something in cmd also.so doing ./NavbarTemplate will resolve your issue as you are importing module from the same folder

0
bpsourav21 On

if your App.jsx and NavbarTemplate.jsx are in the same root directory then just try

import NavbarTemplate from './NavbarTemplate';

it should work

0
Balaganesh On

If you are using a JSX file extension, you need to import like below code,

import NavbarTemplate from './NavbarTemplate.jsx';

If you are using a JS file extension, you need to import like below code,

import NavbarTemplate from './NavbarTemplate';
0
Francesco Orsi On

according with webpack can't find module if file named jsx if you don't want to add .jsx on your import you could add resolve: { extensions: ['.js', '.jsx'] } to your webpack.config.

// ...
    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'
            }
        ]
    },
    resolve: {
        extensions: ['.js', '.jsx']
    }
// ...