Should I transpile my node js code for using ES6?

Viewed 2631

I am using nodejs only for server side microservices, I want to use ES6, I have read that Babel is used to transpile code to ES5 to provide support for browsers. So I need to know if I am not writing this code for client side would I still need to transpile it to run it on V8 engine? Or is it just an extra layer which won't be needed on server side?

4 Answers

You need to use a transpiler if your runtime doesn't support the language features you are using.

The latest version of Node.js has almost complete support for ES6.

If you use features unsupported by whatever version of Node.js you are using, you will need to use a transpiler.

If your server works on the new versions of Node.js and it supports ES6 features, you don't need to transpile your code. Your code only works in the same place, if it is hosted in one place.

Transpiler is used, when your code is written in newer versions, there may be users which use browsers with old versions of Javascript. So the browser need to get the JS code from the hosted place and run locally, here may be errors. In this case you need to use transpiler.

If you are only using the code as a microservice, you don't need to do transpiling. All latest versions of Node.js support es6 by default. You can refer here for more information of what es6 features are currently supported by Node

Node.js es6 documentation

I suggest ues es6/es7 directly in the latest nodejs. Now the latest nodejs almost support most functions except import/export.

If so, you can use arrow function, async await etc.. which can make your code more clear.

Related