Configure Babel for Node
- Install Babel packages using npm:
npm install @babel/core @babel/node @babel/preset-env
- Create a
.babelrc
file in the root folder of your Node.js project with the following configuration:
{
"presets": ["@babel/preset-env"]
}
- Modify your
package.json
file to include the following scripts under"scripts"
:
{
"scripts": {
"start": "nodemon --exec babel-node your_script.js"
}
}
Explanation: 1. Install Babel packages using npm to include the required Babel core and preset-env packages.
Create a
.babelrc
file with the preset-env configuration to specify the Babel preset for targeting specific versions of Node.js or ECMAScript.Modify the
package.json
file to include a script for starting the application using Babel and nodemon for watching file changes and automatically restarting the server.