ES6 is the Node way to go
Tags: node, javascript, codequality, webdev
Published: 2021-07-21 18:36:40

How many of us still use the old ES syntax in our NodeJS apps? 
```javascript
const express = require('express');
const app = express()

app.use(express.static('public'));

app.get('/',function (req,res) {
    res.send('ES what?');
})

app.listen(3000, function () {
  console.log('App listening on port 3000!')
})
```
I bet most of us do, and unfortunately I am equally guilty of this! The reason we continue to use this syntax with node is because most templates and sample codes are based on this. But since the release of [Node 14.x](https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_modules_ecmascript_modules), ES modules is officially supported and stable. But one shouldn't migrate just because its the thing to do. Migrations often involved weighing the pros and cons. After a little research and experimentation, I have concluded that there are actually three big advantages to using ES6's `import` over `require`:
 - `import` helps in selectively loading the pieces of code which are required 
 - `import` also helps in saving memory loaded onto the app
 - `require()`'s loading is synchronous while `import`'s loading can be asynchronous; this provides large apps with a performance edge.

So the natural question is what would it take to use an ES6 syntax in your node project?

Let's get started!

### Setup package.json

One thing to note, Node.js doesn’t support ES6 import directly. If we try to use the keyword `import` for importing modules in Node.js, it will undoubtedly throw an error. For example, if we try to import express module, Node.js will error as follows:
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7t48opxc0o6z8m5yp2qf.png)
 
In order to enable Node support for ES modules we need to tweak the package.json file. In the `package.json` file add `"type": "module"` to the root of the file as such:
```json
//package.json
{
  "name": "index",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "start": "node index.js"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
  "dependencies": {
    "express": "~4.16.1",
  }
}
```

Then run the following command to update changes to package.json
```
npm i
```

### Update your app's code to ES6 syntax

Once we have updated our `package` file we have to make the relevant changes to our app's code as follows:

```javascript
import express from 'express';
  
const app = express();
  
app.get('/',(req,res) => {
    res.send('ES6 is the Node way to go');
})
    
app.listen(3000,() => {
    console.log(`App listening on port 3000!`);
})
```

Run your app and it should work with the updated ES syntax!

I recently update my demo app [Random Cat Facts](https://github.com/mrinasugosh/random-cat-facts-nodejs) to use the new syntax and would recommend checking out the [update ES6 commit diff](https://github.com/mrinasugosh/random-cat-facts-nodejs/commit/7ac49087cdbd6428176588ef9e55dc677ee59ec9) for a real world example of this migration.

Thank you for following along and be sure to look out for my next post!

==== Follow me on Social Media(`@mrinasugosh`) ====
Dev.to: @mrinasugosh 
Github: [@mrinasugosh](https://github.com/mrinasugosh)
Twitter: [@mrinasugosh](https://twitter.com/mrinasugosh)
LinkedIn: [@mrinasugosh](https://www.linkedin.com/in/mrinasugosh/)