5

Is there any way to migrate ES5 code to ES6? I searched for some examples regarding the fact of using latest Node.js and it always gave me an error even with harmony flag. Error included message that there are invalid syntax even for "let" keyword. I wrote to console v8 options and seems everything is turned on..

I'm trying to migrate my framework http://twee.io

Dmitriy
  • 61
  • 1
  • 5
  • The V8 engine behind Node.js (and IO.js) doesn't yet support all of ES6/ES2015. You can find a summary of supported features on https://kangax.github.io/compat-table/es6/#node. – Jonathan Lonowski Feb 26 '15 at 00:33
  • The majority of people using ES6 today are doing so via transpilers like [`babel`](https://babeljs.io/). – loganfsmyth Feb 26 '15 at 02:08
  • For browser based code, webpack will make the transition easier - http://webpack.github.io/. – widged Aug 04 '15 at 02:34
  • For the sole purpose of learning es6, an easy option is electron. See https://github.com/suisho/example-electron-babel (that example includes reactjs support). – widged Aug 04 '15 at 02:44

3 Answers3

3

ES5 code will work along side of ES6 code already. For nodejs just install babel core:

npm install babel-core --save

and you'll also need npm install babel-preset-es2015 --save

create a .babelrc file with the following:

{
    "presets": [ "es2015"],
    "ignore": "node_modules"
}

run your node app via, (assumes your file is app.js)

babel-node app

alternatively you can add require('babel-core/register'); at the top of your node project and any code below will be transpiled with babel...

a recommended way is to create something like main.js with the following contents:

require('babel-core/register');
require('./app.js');

then just run node main.

glued
  • 2,579
  • 1
  • 25
  • 40
1

There isn't any automated tool to convert ES5 to ES6. You need to learn ES6 features and then transform ES5 code to ES6 based on needs.

I have posted quick video tutorial on How to migrate from your existing ES5 code to ES6

https://www.youtube.com/watch?v=vK0mYvK3nkE

Hope this helps someone out there trying to compare and learn

Ajain Vivek
  • 1,111
  • 1
  • 12
  • 20
0

The 2020 answer:

  • npm i -D @babel/cli @babel/core @babel/node @babel/preset-env
  • in package.json scripts property: "babel-node": "babel-node <your arguments here>"

If you want to work with nodemon, inject the following into the scripts as well:

"watch": "nodemon --exec npm run babel-node -- path/to/file.js"

Hope that helps

Hulke
  • 847
  • 5
  • 22