Bryce Bryce - 8 months ago 120
Javascript Question

Converting foreach using Babel

I wrote a script here, that I need to convert from es6 to es5. I wanted to use this opportunity to try out babel, however, I'm running into some issues.

The biggest problem in my code is the foreach:

obj.forEach(block => {
//stuff here

However, after installing babel and using the command: babel myCode.js -o outFile.js

It doesn't change the foreach. Am I doing something wrong? Is there a way to convert foreach lines using babel?


It seams you are not using es2015 preset.

Install es2015 preset package:

npm install babel-preset-es2015

Run babel, using this command:

babel myCode.js -o outFile.js --presets es2015

Or, instead of using --presets es2015 param every time, you can add these code to .babelrc file:

  "presets": ["es2015"]