Convert ES5 to ES6 node js
- How to convert ES5 code to ES6 ?
What is Babel ?
Babel is a JavaScript compiler Babel is a toolchain that is mainly used to convert ECMAScript 2015+ code into a backwards compatible version of JavaScript in current and older browsers or environments.
- install babel as devDependencies
- First download babel-cli using below code to your project.
$ npm install -D babel-cli babel-preset-env babel --save
2. Then create .babelrc
file using below code. This code will create one empty file in your computer.
$ touch .babelrc
3. Now in .babelrc
file write below code.
4. Go to your package.json
file where we’ll create a npm build
script using the babel
command. The babel
command takes two arguments: the path to your ES6 code and a path to where you want your ES5 code to go.
5. Now run build command.
$ npm run build
6. Now you are good to go. now you can import rather then require in your node program. If you face import error then install npm i esm
or write “type”: “module”
in your package.json
file. if any issue occur please let me know.
Parth over and out.🖖