ES6 and Impact to Node.js

When Node.js came into existence, an ES Modules proposal did not exist. Node.js decided to use CommonJS Modules. But now, Node.js has evolved the specification to create a very large JavaScript ecosystem and CommonJS as an organization is no longer an active concern. As a result the Node/npm JavaScript module ecosystem spans both server and client and is growing rapidly.Node.js today is in a rapid development with its currently 4.X and 5.X LTS (Long Term Support) versions. Minor updates are coming within days and it is becoming a real strong and well supported server-side language.In Node.js development with ECMAScript 6 (ES6) syntax, here is a little list of some things you should do to quickly improve your Node.js development.1. Avoid “var” completely: The “var” keyword is commonly used to define variables, but is accessible out of the scope it was in defined. To keep the global variable names as minimal as possible, avoid using “var”. Instead, “const” and “let” should be used and this also avoids issues with the same variable names already assigned values to the variable.2. Assume synchronous codes in asynchronous functions: With the new generator syntax of ES6, it allows you to use generators to write code synchronously in an asynchronous function. This is made possible by using the “yield” keyword. A generator in a lay man form simply does the job of when it is done executing the code and there was no error at all, it is returned and the function it was called in continues. Generators are defined by adding an asterisk before the function name or after the function keyword. To yield generators, you can only use them within other generators or with a library to execute them.3. Structure by “destructuring”: To improve readability of code, using destructuring of Objects and Arrays to improve readability and performance in some way. However, you must keep it in mind that the variables you define when you destructure and Object need to be exactly the same property as within the Object itself. In the case of destructuring an Array and use brackets instead of braces. The first variable corresponds with the value of the first index, the second variable with the second index and so on.4. Use as little indentations as possible to enhance readability: As ironic as this may sound, try as much as possible to write code with minimal indentations. This can be achieved by various methods such as not using an “else” statement when the block of code inside the last “if” statement of the function returns. Another method to reduce indentation is splitting a function with more than one indentation into more functions.5. Use appropriate logging properly: To easily determine the processes within your system, use proper logging to identify problems. This can also be achieved via various methods and I will cite a few. You can use an advanced logger. A lot of these are available and a good example is Tracer. You can also log on any external network call or use logging codes or standard messages on errors.6. Write encapsulated with “dependency injection”: As documented in the Node.js documentation, the instance of an imported module is shared if imported multiple times within the same project. However, dependency injection encapsulates your pointer to the module in a function. This makes your pointers module-scoped and not global-scoped.7. Create “classes” for important purposes: In ES6, classes is a new syntax to create multiple objects with the same functions and attributes. It is not the class that we know in Java but simply a new way to define prototype-based inheritance.


quick inquiry

our latest blogs