How to use ES6+ features in nodejs

This article is about how to use es6+ in nodejs project

Initialising project with npm

npm init -y

Installing babel plugins for es6+ features

npm i -D @babel/cli @babel/core @babel/plugin-proposal-class-properties @ba…


This content originally appeared on DEV Community and was authored by sakethk

This article is about how to use es6+ in nodejs project

Initialising project with npm

npm init -y

Installing babel plugins for es6+ features

npm i -D @babel/cli @babel/core @babel/plugin-proposal-class-properties @babel/plugin-transform-runtime @babel/preset-env 

Adding babel support for project

touch .babelrc

Paste the following content in .babelrc

{
    "presets": ["@babel/preset-env"],
    "plugins": ["@babel/plugin-proposal-class-properties", "@babel/transform-runtime"]
}

Babel is not a compiler or interpreter it is just a transpiler so we have to transpile es6 to es5 using babel then we have to execute that transpiled code. For that we can write npm script.

Add the following script to package.json

"build": "babel src -d dist",
"start": "npm run build && node dist"

Now create src folder and start writing es6+ code inside that folder. Run npm start it will create dist folder inside that folder we can find transpiled code.

Cheers!!!


This content originally appeared on DEV Community and was authored by sakethk


Print Share Comment Cite Upload Translate Updates
APA

sakethk | Sciencx (2021-10-29T17:06:49+00:00) How to use ES6+ features in nodejs. Retrieved from https://www.scien.cx/2021/10/29/how-to-use-es6-features-in-nodejs/

MLA
" » How to use ES6+ features in nodejs." sakethk | Sciencx - Friday October 29, 2021, https://www.scien.cx/2021/10/29/how-to-use-es6-features-in-nodejs/
HARVARD
sakethk | Sciencx Friday October 29, 2021 » How to use ES6+ features in nodejs., viewed ,<https://www.scien.cx/2021/10/29/how-to-use-es6-features-in-nodejs/>
VANCOUVER
sakethk | Sciencx - » How to use ES6+ features in nodejs. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/29/how-to-use-es6-features-in-nodejs/
CHICAGO
" » How to use ES6+ features in nodejs." sakethk | Sciencx - Accessed . https://www.scien.cx/2021/10/29/how-to-use-es6-features-in-nodejs/
IEEE
" » How to use ES6+ features in nodejs." sakethk | Sciencx [Online]. Available: https://www.scien.cx/2021/10/29/how-to-use-es6-features-in-nodejs/. [Accessed: ]
rf:citation
» How to use ES6+ features in nodejs | sakethk | Sciencx | https://www.scien.cx/2021/10/29/how-to-use-es6-features-in-nodejs/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.