1

I trying to import a react component to jsx file but it throws this exception: import error react

This is my main code:

import React, { Component } from "react";
import Sidebar from "./Sidebar";

class App extends Component{
    render(){
        return (
            <Sidebar/>
        );
    }
}

ReactDOM.render(
    <App />, 
    document.getElementById("root")
);

and this is my Sidebar component:

import React, { Component } from "react";

export default class Sidebar extends Component{
    render(){
        return (
            <h1>Hello Sidebar</h1>
        );
    }
}

My folders structure:

Folder Structure react

7
  • 8
    please share folder structure Commented Oct 17, 2018 at 7:02
  • 2
    PLEase check you path for SideBar component Commented Oct 17, 2018 at 7:03
  • Likely a path issue here. Commented Oct 17, 2018 at 7:05
  • Sidebar.js and App.js must be the same directory Commented Oct 17, 2018 at 7:08
  • 2
    Probably import Sidebar from "./Sidebar.jsx"; will solve your issue.You might not have configured extensions to be resolved in webpack Commented Oct 17, 2018 at 7:13

2 Answers 2

3

I post simpler version which I know does work:

./index.js :

import React from 'react';
import ReactDOM from 'react-dom';
import Application from './components/Application'

ReactDOM.render(<Application />, document.getElementById('root'));

./components/Application :

import React from 'react';

class Application extends React.Component {
  render() {
    return (
      <div className="App">
        My Application!
      </div>
    );
  }
}

export default Application;

This should be everything needed to make it work.

If you want to shorten the above even more, by removing the export line at the bottom, a less traditional approach would be defining the class like this...

export default class Application extends React.Component {...}
Sign up to request clarification or add additional context in comments.

1 Comment

I try that but it throws me previous exception. module not found
2

Looks like you haven't added rule for .jsx file in webpack.config.js.

Since you have both .js and .jsx files you need to tell webpack to load files with extension .js and .jsx. Add below config to webpack.config.js in rules section

    {
        //tell webpack to use jsx-loader for all *.jsx files
        test: /\.(js|jsx)$/,
        exclude: /node_modules/,
        loader: "babel-loader"
    }

And add extensions like

   resolve: {
    modules: [
        path.resolve("./src"),
        path.resolve("./node_modules")
    ],
    extensions: [".js", ".jsx"]
  }

Below is the working webpack.config.js file for your ref

module.exports = {
    target: "web",
    entry: [
        "whatwg-fetch",
        'webpack-dev-server/client',
        'webpack/hot/only-dev-server',
        'babel-polyfill',
        "./src/index.js"
        ],
    output: {
        path: __dirname + 'build',
        publicPath: '/',
        filename: "bundle.js"
    },
    plugins: [new HtmlWebpackPlugin({
        template: "index.html"
    }),
    new CompressionPlugin({
        asset: "[path].gz[query]",
        algorithm: "gzip",
        test: /\.js$|\.jsx$|\.css$|\.html$/,
        threshold: 10240,
        minRatio: 0.8
    }),
    new webpack.HotModuleReplacementPlugin(),
    // enable HMR globally

    new webpack.NoEmitOnErrorsPlugin()
    ],
    module: {
        rules: [
            {
                //tell webpack to use jsx-loader for all *.jsx files
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                loader: "babel-loader"
            },
            {
                test: /\.css$/,
                loader: "style-loader!css-loader"
            },
            {
                test: /\.(png|jpg|jpeg|gif|svg|woff|woff2)$/,
                use: [
                  {
                    loader: 'file-loader',
                    options: {}
                  }
                ]
            },
            {
                test: /\.(eot|ttf)$/,
                loader: "file-loader",
            },
            {
                test: /\.scss$/,
                loaders: ["style-loader", "css-loader", "sass-loader"]
            }
        ]
    },
    resolve: {
        modules: [
            path.resolve("./src"),
            path.resolve("./node_modules")
        ],
        extensions: [".js", ".jsx"]
    },
    devServer: {
        watchOptions: {
        // Needed for Windows Subsystem for Linux dev environment:
            poll: true
        },
        contentBase: './build'
    },
    devtool: "cheap-module-eval-source-map",
    node: {
        child_process : "empty",
        fs: "empty"
    }
};

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.