1

I am building a React JS project. In my project, I am using the arrow functions which are the new ES6 syntax. But it is throwing an error when I run the project. I am using Webpack to compile my code.

This is my webpack.config.js file.

const HTMLWebPackPlugin = require('html-webpack-plugin');
const path = require('path');

module.exports = {
    entry: [ 'babel-polyfill', './src/index.js' ],
    output: {
        path: path.resolve(__dirname, "build"),
        filename: "bundle.js"
    },
    module:{
        rules:[
            {
                test: /\.(js|jsx)$/,
                exclude: /node_modules/,
                use: ['babel-loader'],
            },
            {
                test: /\.scss$/,
                exclude: /node_modules/,
                use: ['style-loader', 'css-loader', 'sass-loader']
            }
        ],
    },
    resolve: {
        extensions: ['*', '.js', '.jsx'],
    },
    plugins: [
        new HTMLWebPackPlugin({ template: path.join(__dirname, '/build/index.html') })
    ],
    devServer:{
        port:5200
    }
};

I have .babelrc file right inside the project root folder with the following content.

{
  "presets": [
    "es2015", "react",
    "@babel/preset-env",
    "@babel/preset-react"
  ]
}

In a React component, I am declaring an arrow function as follow.

addBackDrop = e => {
        if(this.state.showDatePicker && !ReactDOM.findDOMNode(this).contains(e.target)) {
            this.showDatePicker(false);
        }
    }

The function is within the class component.

When I run, "npm run dev", I am getting the following error.

ERROR in ./src/index.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
Error: Plugin/Preset files are not allowed to export objects, only functions. In C:\Users\Acer\Desktop\way-ui-react\node_modules\babel-preset-es2015\lib\index.js

What is wrong with my configuration and how can I fix it?

3
  • 1
    Does this help your issue ? stackoverflow.com/a/42064325/11866037 Commented Nov 14, 2020 at 15:15
  • It turns out that it is not the issue with "npm run dev". It runs fine. The issue pops up when it renders the component that is using the arrow function. Commented Nov 14, 2020 at 15:24
  • @KaungKhantZaw. I just posted an answer. Cheers for jumping in. That was helpful. Commented Nov 14, 2020 at 15:46

3 Answers 3

1

is this a functional component? if so you need to add const before the function name

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

1 Comment

No. It is a class component.
0

This will solve the issue. .babelrc file

{
  "presets": [
    "@babel/preset-env",
    "@babel/preset-react"
  ],
  "plugins": [
    "babel-plugin-transform-class-properties"
  ]
}

Comments

0

Hi I think that the issue is that you missed to put the parenthesis " addBackDrop = (e) => "

addBackDrop = (e) => {
        if(this.state.showDatePicker && !ReactDOM.findDOMNode(this).contains(e.target)) {
            this.showDatePicker(false);
        }
    }

1 Comment

In most dev setups the parenthesis are not required for single argument arrow functions

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.