Babel giving Unexpted token while building

15,509

Solution 1

I found that the error is because of older version of babel which doesn't handle newer versions of react code.

Here's the fix :

My problem was of older babel version fixed easily by installing:

npm i -D @babel/plugin-proposal-class-properties \
  @babel/preset-react \
  @babel/preset-env \
  @babel/core \
  @babel/plugin-transform-runtime \

In .babelrc file :

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

Now babel built it successfully after this.

Solution 2

Make sure you are not using both v7 and v6 branch og babel. The "@babel/core" is the 7x branch and the "babel/core" is the 6x branch, you should not have both installed at once!

Share:
15,509

Related videos on Youtube

Natesh bhat
Author by

Natesh bhat

I love programming , and learning different technologies of Computer Science. Am a singer and I play Tabla. With strong passion in the field of Computer Science , I love programming and building projects along as I learn. I like the immensely helpful community of StackOverflow which has enabled me to contribute to the vast programmer base with what I can over the years.

Updated on June 04, 2022

Comments

  • Natesh bhat
    Natesh bhat almost 2 years

    I am trying to build my react library , and npm build gives this error . what is causing this error and how to fix it ?

        src/lib/CircularProfiles.js -> dist/CircularProfiles.js
        SyntaxError: src/lib/Github.js: Unexpected token (14:10)
          12 | class GithubProfileBar extends Component {
          13 |
        > 14 |     state = {
             |           ^
          15 |         totalRepos: 0,
          16 |         totalStars: 0,
          17 |     }
    
    npm ERR! code ELIFECYCLE
    npm ERR! errno 1
    npm ERR! [email protected] build: `rm -rf dist && NODE_ENV=production babel src/lib --out-dir dist --copy-files --ignore __tests__,spec.js,test.js,__snapshots__`
    npm ERR! Exit status 1
    npm ERR!
    npm ERR! Failed at the [email protected] build script.
    npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
    
    npm ERR! A complete log of this run can be found in:
    npm ERR!     /home/natesh/.npm/_logs/2018-12-26T03_51_21_931Z-debug.log
    

    My .babelrc file :

    {
        "presets": [
            "es2015",
            "react"
        ]
    }