Babel 6 CLI: Unexpected token export?

前端 未结 2 1467
失恋的感觉
失恋的感觉 2020-12-25 09:30

I\'m trying to run Babel through it\'s CLI using babel-node but I keep getting the Unexpected token export error. I understand that Babel 6 is all

相关标签:
2条回答
  • 2020-12-25 10:06

    I received the same error, but my webpack/babel configs looked correct. By trial and error, I replaced export myFunction with export default myFunction and the error got resolved.


    Later, I realized that the correct way of exporting is export {myFunction}. I implemented it and everything works fine.

    0 讨论(0)
  • 2020-12-25 10:10

    The easiest way to get started is to use a preset.

    First let's install our dependencies:

    $ npm install --save-dev babel-cli babel-preset-es2015
    

    Then add a build script to your package.json that runs Babel: (this is important because it will use your local version of babel-cli instead of a globally installed one)

    "build": "babel input.js"
    

    Your package.json should look like this:

    {
      "name": "my-module",
      "devDependencies": {
        "babel-cli": "^6.x.x",
        "babel-preset-es2015": "^6.x.x"
      },
      "scripts": {
        "build": "babel input.js -o compiled.js"
      }
    }
    

    Finally you want to update your local .babelrc like this:

    {
      "presets": ["es2015"]
    }
    

    Then you run npm run build and you're all set to go.

    Also, does Babel 6's CLI have a global .babelrc option? It seems tedious if I have to install the plugins for every project that requires it...

    That's a bad idea as it means you can't ever update it without updating every single one of your projects code. Having local versions means this potential error is less likely to occur.

    0 讨论(0)
提交回复
热议问题