Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module not found when import .jsx file

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

like image 320
Rido Avatar asked Aug 29 '17 04:08

Rido


People also ask

How do I fix module not found error in React?

To solve the "Module not found: Can't resolve" error in React, make sure to install the package from the error message if it's a third-party package, e.g. npm i somePackage . If you got the error when importing local files, correct your import path.

Do you need to import React for JSX?

1 Answer. Show activity on this post. You no longer need to import React from "react" . Starting from the release 17 of React, JSX is automatically transformed without using React.

Can we use JSX in react native?

React and React Native use JSX, a syntax that lets you write elements inside JavaScript like so: <Text>Hello, I am your cat! </Text> . The React docs have a comprehensive guide to JSX you can refer to learn even more. Because JSX is JavaScript, you can use variables inside it.

Can I write JSX in JS file?

JSX allows us to write HTML elements in JavaScript and place them in the DOM without any createElement() and/or appendChild() methods. JSX converts HTML tags into react elements. You are not required to use JSX, but JSX makes it easier to write React applications.


3 Answers

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';
like image 138
Miguel Avatar answered Sep 19 '22 12:09

Miguel


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']
    }
// ...
like image 41
Francesco Orsi Avatar answered Sep 23 '22 12:09

Francesco Orsi


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

import NavbarTemplate from './NavbarTemplate';

it should work

like image 45
bpsourav21 Avatar answered Sep 19 '22 12:09

bpsourav21