Can not get babel version 7 @babel/register to work

1.2k Views Asked by At

I am trying out babel@7's @babel/register, but can't seem to get it to work. My package.json are following:

{
  "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"
  }
}

and es6 file that require('@babel/register') is:

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()

Note that I purposely use node version 6 to check if babel actually transpile my es6 script.

and I get:

$ nvm use 6
Now using node v6.9.1

$ npm run start

> [email protected] 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! [email protected] start: `node index.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the [email protected] start script 'node index.js'.

link to repo: https://github.com/ApolloTang/trying-out-babel-register-v7

1

There are 1 best solutions below

0
apollo On

I have figured it out: for some reason the code to be transpiled has to be in a file required (see comment bellow):

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')

I have also include the working code in repo, link: https://github.com/ApolloTang/trying-out-babel-register-v7/tree/solved