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?