首页 文章

无法获得babel版本7 @ babel /注册工作

提问于
浏览
0

我正在尝试babel @ 7的@ babel / register,但似乎无法让它工作 . 我的package.json如下:

{
  "name": "trying-out-babel-register-v7",
  "version": "1.0.0",
  "engines": {
    "node": "~6.0.0"
   },
  "license": "MIT",
  "dependencies": {
    "@babel/core": "^7.2.0",
    "@babel/register": "^7.0.0"
  },
  "devDependencies": {
    "@babel/preset-env": "^7.2.0"
  },
  "scripts": {
    "start": "node index.js"
  }
}

和es6文件('@ babel / register')是:

require('@babel/register')({
  presets: [
    [
       "@babel/env",
       {
         module: false,
         targets: { "node": process.versions.node },
         useBuiltIns: "usage"
       }
    ]
  ]
});

const f = () =>{ console.log('arrow function work')}
f()

const a = {'a': 'a'};

const b = {
  'b':'b',
  ...a
};
console.log(b)

class A {
  constructor() {
    console.log('hello class')
  }
}
const k = new A()

请注意,我故意使用节点版本6来检查babel是否实际上转换了我的es6脚本 .

我得到:

$ nvm use 6
Now using node v6.9.1

$ npm run start

> trying-out-babel-register-v7@1.0.0 start /Users/apollotang/Desktop/trying-out-babel-register-v7
> node index.js

/Users/apollotang/Desktop/trying-out-babel-register-v7/index.js:21
   ...a
   ^^^
SyntaxError: Unexpected token ...
   at Object.exports.runInThisContext (vm.js:76:16)
   at Module._compile (module.js:542:28)
   at Object.Module._extensions..js (module.js:579:10)
   at Module.load (module.js:487:32)
   at tryModuleLoad (module.js:446:12)
   at Function.Module._load (module.js:438:3)
   at Module.runMain (module.js:604:10)
   at run (bootstrap_node.js:394:7)
   at startup (bootstrap_node.js:149:9)
   at bootstrap_node.js:509:3
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! trying-out-babel-register-v7@1.0.0 start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the trying-out-babel-register-v7@1.0.0 start script 'node index.js'.

链接到回购:https://github.com/ApolloTang/trying-out-babel-register-v7

1 回答

  • 0

    我已经弄清楚了:出于某种原因,要编译的代码必须在一个文件中(参见下面的评论):

    require('@babel/register')({
      presets: [
        [
           "@babel/env",
           {
             // module: false,      // <--- typo, not module
             modules: "commonjs",   // <--- must transpile to commonjs module
             targets: { "node": process.versions.node },
             useBuiltIns: "usage"   // <--- not sure if this work
           }
        ]
      ]
    });
    
    const f = () =>{ console.log('arrow function work')}
    f()
    
     /*
      * does not work here, but works in the
      * required file script-1.js
      *
      * const a = {'a': 'a'};
      *
      * const b = {
      *   'b':'b',
      *   ...a
      * };
      * console.log(b)
      */
    
    class A {
      constructor() {
        console.log('hello class')
      }
    }
    const k = new A()
    
    // import someScript from  './script-1.js'; // <-- import does not work here but will work in ./script-1.js
    require( './script-1.js')
    

    我还在repo中包含了工作代码,链接:https://github.com/ApolloTang/trying-out-babel-register-v7/tree/solved

相关问题