DEV Community

loading...

How I started Using ES6 Modules in Node JS

chrisdevcode profile image ChrisAchinga ・2 min read

How I started Using ES6 Modules in Node JS

A short guide on how I started using ES6 Modules when using Node.

I love the EcmaScript Module syntax and I use it almost in all my code and practices.

I will use the example from Express Introduction - MDN

So, create a new folder (node-es6):

mkdir node-es6
Enter fullscreen mode Exit fullscreen mode

Inside the folder, initialize a node application by:

npm init -y
Enter fullscreen mode Exit fullscreen mode

Now open the folder using your favorite text editor.

Create a new file hello.js and paste the code:

// Load HTTP module
const http = require("http");

const hostname = "127.0.0.1";
const port = 8000;

// Create HTTP server
const server = http.createServer((req, res) => {

   // Set the response HTTP header with HTTP status and Content type
   res.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body "Hello World"
   res.end('Hello World\n');
});

// Prints a log once the server starts listening
server.listen(port, hostname, () => {
   console.log(`Server running at http://${hostname}:${port}/`);
})
Enter fullscreen mode Exit fullscreen mode

Run the file to ensure that it's okay:

node hello.js
Enter fullscreen mode Exit fullscreen mode

If the message shows on the terminal:

Server running at http://127.0.0.1:8000/
Enter fullscreen mode Exit fullscreen mode

Then it's running.

Using ES6

It's simple to get started, head over to the package.json file, and add the line:

"type": "module",
Enter fullscreen mode Exit fullscreen mode

Your updated file should be like this:

{
  "name": "node-es6",
  "version": "1.0.0",
  "type": "module",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC"
}
Enter fullscreen mode Exit fullscreen mode

The last step would be to update our js file to use es6 modules"

// Load HTTP module
import http from "http";

const hostname = "127.0.0.1";
const port = 8000;

// Create HTTP server
const server = http.createServer((req, res) => {

   // Set the response HTTP header with HTTP status and Content type
   res.writeHead(200, {'Content-Type': 'text/plain'});

   // Send the response body "Hello World"
   res.end('Hello World\n');
});

// Prints a log once the server starts listening
server.listen(port, hostname, () => {
   console.log(`Server running at http://${hostname}:${port}/`);
})
Enter fullscreen mode Exit fullscreen mode

Note that I changed

// Load HTTP module
const http = require("http");
Enter fullscreen mode Exit fullscreen mode

To

// Load HTTP module
import http from "http";
Enter fullscreen mode Exit fullscreen mode

Run the file to make sure that everything worked as planned.

That's it, Tschuss!!

Discussion (2)

Collapse
patarapolw profile image
Pacharapol Withayasakpunt

Ok. I know I can, but why? What's the benefit?

Collapse
chrisdevcode profile image
ChrisAchinga Author

Es6 new syntax makes the code shorter and more readable. Like you'll write more for less, that's of you love the DRY principal. It alsp gives you more control over the program you creating.

Forem Open with the Forem app