Monday, January 18, 2021

Difference between Node JS and ES6 modules

The modules used in Node.js follow a module specification known as the CommonJS specification.

The recent updates to the JavaScript programming language, in the form of ES6, specify changes to the language, adding things like new class syntax and a module system. This module system is different from Node.js modules. A module in ES6 looks like the following:


// book.js

const favoriteBook = {

    title: "The Guards",

    author: "Ken Bruen"

}


// a Book class using ES6 class syntax

class Book {

    constructor(title, author) {

        this.title = title;

        this.author = author;

    }


    describeBook() {

        let description = this.title + " by " + this.author + ".";

        return description;

    }

}


// exporting looks different from Node.js but is almost as simple

export {favoriteBook, Book};


To import this module, we'd use the ES6 import functionality, as follows.


/ library.js


// import the book module

import {favoriteBook, Book} from 'book';


// create some books and get their descriptions

let booksILike = [

    new Book("Under The Dome", "Steven King"),

    new Book("Julius Ceasar", "William Shakespeare")

];


console.log("My favorite book is " + favoriteBook + ".");

console.log("I also like " + booksILike[0].describeBook() + " and " + booksILike[1].describeBook());


ES6 modules look almost as simple as the modules we have used in Node.js, but they are incompatible with Node.js modules. This has to do with the way modules are loaded differently between the two formats. If you use a compiler like Babel, you can mix and match module formats. If you intend to code on the server alone with Node.js, however, you can stick to the module format for Node.js which we covered earlier.




References:

https://stackabuse.com/how-to-use-module-exports-in-node-js/Difference between Node JS and ES6 modules 


The modules used in Node.js follow a module specification known as the CommonJS specification.

The recent updates to the JavaScript programming language, in the form of ES6, specify changes to the language, adding things like new class syntax and a module system. This module system is different from Node.js modules. A module in ES6 looks like the following:


// book.js

const favoriteBook = {

    title: "The Guards",

    author: "Ken Bruen"

}


// a Book class using ES6 class syntax

class Book {

    constructor(title, author) {

        this.title = title;

        this.author = author;

    }


    describeBook() {

        let description = this.title + " by " + this.author + ".";

        return description;

    }

}


// exporting looks different from Node.js but is almost as simple

export {favoriteBook, Book};


To import this module, we'd use the ES6 import functionality, as follows.


/ library.js


// import the book module

import {favoriteBook, Book} from 'book';


// create some books and get their descriptions

let booksILike = [

    new Book("Under The Dome", "Steven King"),

    new Book("Julius Ceasar", "William Shakespeare")

];


console.log("My favorite book is " + favoriteBook + ".");

console.log("I also like " + booksILike[0].describeBook() + " and " + booksILike[1].describeBook());


ES6 modules look almost as simple as the modules we have used in Node.js, but they are incompatible with Node.js modules. This has to do with the way modules are loaded differently between the two formats. If you use a compiler like Babel, you can mix and match module formats. If you intend to code on the server alone with Node.js, however, you can stick to the module format for Node.js which we covered earlier.




References:

https://stackabuse.com/how-to-use-module-exports-in-node-js/

No comments:

Post a Comment