Day 4 of NodeJS||ES6 Import||Module

Hey reader👋Hope you are doing well😊
In the last post we have discussed about modules in NodeJS. In this post we are going to an another way of using modules in NodeJS.
So let’s get started🔥

Importing a Module

In the last post we have discus…


This content originally appeared on DEV Community and was authored by Akshat Sharma

Hey reader👋Hope you are doing well😊
In the last post we have discussed about modules in NodeJS. In this post we are going to an another way of using modules in NodeJS.
So let's get started🔥

Importing a Module

In the last post we have discussed that everything in NodeJS is a module and we can use any module using require() method. Now instead of using this old school methodology for using modules let's explore a new method of using a module in file.
With the release of ES6 (6th edition of the ECMAScript language) the standardized syntax for importing and exporting modules in JavaScript was also released.
Using import we can use any module in a file. Though NodeJS doesn't directly support the ES6 import directly.
There are two ways of using import in NodeJS-:

  1. Modifying package.json file-:

We can modify the package.json file in order to use import in NodeJS.
Let me explain you what package.json is-:
package. json is a metadata file in a NodeJS project that describes the project's dependencies, scripts, configuration, and other details. It typically contains information about the project such as its name, version, author, and license. It also lists the project's dependencies on other Node.
Image description
You can easily create package.json file by using command-:
npm init.
To use import in NodeJS add following lines to package.json-:
"type":"module"
Image description
Now let's use module in script.js using import-:
Image description

  1. By using esm module-:

We can install esm module using command-:
npm install esm

ECMAScript Modules (ESM) is a specification for using Modules in the Web. It's supported by all modern browsers and the recommended way of writing modular code for the Web.

Using esm it is not needed to add "type":"module" in package.json, we can directly use import statement.

Advantages of using import

  • import helps in selectively loading the pieces of code that are required which helps in saving memory.So we don't need complete module here we only import those functionalities which are required.

  • In case of require loading is synchronous whereas import can be asynchronous so it performs better than required.

Quick Tip!

  • You also have to modify your custom module also as follows-:
    Image description
    exports will not going to work here.

  • require was a method defined in CommonJS and import in ES6.

So this was all about import. I hope you have understood this blog. In the next blog we are to read about REPL. Till then stay connected and don't forget to follow me.
Thankyou🤍


This content originally appeared on DEV Community and was authored by Akshat Sharma


Print Share Comment Cite Upload Translate Updates
APA

Akshat Sharma | Sciencx (2024-07-22T16:28:39+00:00) Day 4 of NodeJS||ES6 Import||Module. Retrieved from https://www.scien.cx/2024/07/22/day-4-of-nodejses6-importmodule/

MLA
" » Day 4 of NodeJS||ES6 Import||Module." Akshat Sharma | Sciencx - Monday July 22, 2024, https://www.scien.cx/2024/07/22/day-4-of-nodejses6-importmodule/
HARVARD
Akshat Sharma | Sciencx Monday July 22, 2024 » Day 4 of NodeJS||ES6 Import||Module., viewed ,<https://www.scien.cx/2024/07/22/day-4-of-nodejses6-importmodule/>
VANCOUVER
Akshat Sharma | Sciencx - » Day 4 of NodeJS||ES6 Import||Module. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/22/day-4-of-nodejses6-importmodule/
CHICAGO
" » Day 4 of NodeJS||ES6 Import||Module." Akshat Sharma | Sciencx - Accessed . https://www.scien.cx/2024/07/22/day-4-of-nodejses6-importmodule/
IEEE
" » Day 4 of NodeJS||ES6 Import||Module." Akshat Sharma | Sciencx [Online]. Available: https://www.scien.cx/2024/07/22/day-4-of-nodejses6-importmodule/. [Accessed: ]
rf:citation
» Day 4 of NodeJS||ES6 Import||Module | Akshat Sharma | Sciencx | https://www.scien.cx/2024/07/22/day-4-of-nodejses6-importmodule/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.