4

I am running a React project using npm. After hours of research and experimenting, everywhere says I have to add the following code to my ".babelrc" file, which I do not have in my directory and cannot create:

{
  "plugins": [
    ["@babel/plugin-proposal-class-properties", { "loose": true }]
  ]
}

But this leads to the following error when I run the code:

ERROR in ./src/components/NavBar/Menu.js
Module build failed (from ./node_modules/babel-loader/lib/index.js):
SyntaxError: /Users/ymoondhra/Desktop/yt-web/src/components/NavBar/Menu.js:
Support for the experimental syntax 'classProperties' isn't currently enabled
 (20:10):
  18 |     }
  19 | 
> 20 |     fast = () => {
     |          ^
  21 |         this.speed = 100;
  22 |     }
  23 | 
Add @babel/plugin-proposal-class-properties (https://git.io/vb4SL) to the 'plugins' 
section of your Babel config to enable transformation.

1 Answer 1

9

Found the solution to my question after 3 hours: –create a file called "babel.config.js" in the root directory. –put this in the file. –rerun the program (i.e. npm start).

module.exports = {
  presets: [ "@babel/preset-env", "@babel/preset-react" ],
  plugins: [ "@babel/plugin-transform-arrow-functions", "@babel/plugin-proposal-    class-properties" ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

does it need extra set up? i.e package.json
Took me ages to make it work. I was just changing the filename but was missing the modules.exports. Great post.
Does it overwrite .babelrc?

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.