DEV Community

sakethk
sakethk

Posted on • Edited on

4 1

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
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Adding babel support for project

touch .babelrc
Enter fullscreen mode Exit fullscreen mode

Paste the following content in .babelrc

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

Enter fullscreen mode Exit fullscreen mode

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"
Enter fullscreen mode Exit fullscreen mode

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!!!
You can now extend your support by buying me a Coffee.

Buy Me A Coffee

Tiugo image

Modular, Fast, and Built for Developers

CKEditor 5 gives you full control over your editing experience. A modular architecture means you get high performance, fewer re-renders and a setup that scales with your needs.

Start now

Top comments (1)

Collapse
 
doehl profile image
Mathias Døhl

NodeJS has a very good support for ES6+ features, in the latest versions.

One of the nice things with NodeJS, is that you yourself can control the version being used. The node team has become amazing, at keeping up with the newest features of the language. So if you can keep up with the latest nodeJS releases, are you up for greate support for even the latest ES2021 and ES2022 features. No need for babel.

kangax.github.io/compat-table/es20...

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Engage with a wealth of insights in this thoughtful article, valued within the supportive DEV Community. Coders of every background are welcome to join in and add to our collective wisdom.

A sincere "thank you" often brightens someone’s day. Share your gratitude in the comments below!

On DEV, the act of sharing knowledge eases our journey and fortifies our community ties. Found value in this? A quick thank you to the author can make a significant impact.

Okay