Unexpected reserved word 'import' when using babel

后端 未结 3 2086
有刺的猬
有刺的猬 2021-01-03 20:41

Using Babel in my NodeJSv4.1.1 code.

Got the require hook in:

require(\"babel-core/register\");

$appRoot = __dirname;

module.exports = require(\".         


        
相关标签:
3条回答
  • 2021-01-03 21:24

    Sounds like you aren't using the right presets. As of babel 6, the core babel loader no longer includes the expected ES6 transforms by default (it's now a generic code transformer platform), instead you must use a preset:

    require('babel-register')({
            "presets": ["es2015"]
    });
    

    You will also need to install the preset package:

    npm install --save-dev babel-preset-es2015
    
    0 讨论(0)
  • 2021-01-03 21:30

    It seems that this file is not being transpiled. Is this subsequently loaded .js file in the node_modules directory? If so, you need to:

    require("babel-core/register")({
      // This will override `node_modules` ignoring - you can alternatively pass
      // an array of strings to be explicitly matched or a regex / glob
      ignore: false
    });
    

    By default all requires to node_modules will be ignored. You can override this by passing an ignore regex

    https://babeljs.io/docs/usage/require/

    0 讨论(0)
  • 2021-01-03 21:36

    I was hitting the problem when trying to run tests via mocha, and I solved it by putting this in my package.json file:

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

    I'm not completely clear on how this works. I'm running tests like this:

    mocha --compilers js:babel-core/register --require ./test/test_helper.js --recursive
    

    Eventually, this will all make sense I suppose.

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