Realtime and Scalable Web Applications

Import module in node.js: how to import and export modules

programming-logic-and-debugging

A module is formed by a collection of computer programs which collectively work towards the main goal of the module. We can also term module as a library (internal or external, read about node.js internal core modules here, read about types of modules in node.js here). In this post we will see the process of how the import module in node.js as well as how the export module in node.js works.

Several times it happens that we need to use a specific kind of module into our program.

Getting started with programming in Node.js

What is a module in terms of computer programming?

In general terms we can describe a module as a library containing some code that helps us to connect our site to Facebook or google logins or signups as we can see various websites today that we have an option to signup and login via these platforms. It is not that these website developers have to again and again recreate the same piece of coding instead a module containing a common code which can be simply plugged in and it does rest of the work.

Similarly we will have at many times import a lot of modules in our code sometimes to create and run a server, import/export  a file or a module itself, sometimes to make a tls/ssl secure connection or performing authorisation and authentication(like sign up or logins in our website).

Thus In a single piece of code called a module, related code is collected. This can be understood as consolidating all associated functions into one file when building a module. 

Importing and exporting modules in node.js

Modern V8 implementations are supported by Node.js. We make that new features from the JavaScript ECMA-262 specification, as well as ongoing performance and stability enhancements, are delivered to Node.js developers in a timely manner by staying up to date with the most recent releases of this engine. 

There are three categories for shipping, staged, and in-progress features for all of ECMAScript 2015 (ES6) features: 

On Node.js, all shipping features that V8 deems stable are enabled by default and don’t need any kind of runtime flags. Staged features, which are nearly finished features that the V8 team does not deem reliable, demand a runtime flag: —harmony. Features that are currently in development can be individually activated by their corresponding harmony flag, though this is strongly discouraged unless for testing purposes. Note that these flags are displayed by V8 and may change without prior warning of deprecation. 

ES6 currently comes with native support for import statements. 

Export module in node.js

In this section we will inspect about the process of how the export module in node.js works. Lets consider a piece of code where we export a certain function in a file named readFile1.js –

export default function readFunction(parameters){

const line = "this is printed in the read function";

parameters = line + parameters;

console.log(parameters);

}

export default function readFunction2(parameters){

console.log(parameters);

}

Here export default is a node.js function which is used to export a particular module. We have named our module as readFunction and readFunction2 module.

Import module in node.js

There are three ways to import a module in node.js. We will try to inspect that how the import module in node.js works. We can import this specific function written above, so lets consider if there is a file called application.js where we want to use them. 

import readFunction from ‘./readFile1‘;

readFunction(“ —this line is added when readFile1 is called—”);
The output of this program will be “this is printed in the read function —this line is added when readFile1 is called—”

What’s happening above is we created 2 files named readFile1.js and application.js

In readFile1.js we defined a function readFunction which takes in parameters as input to the function.(Please read here about the details of how a function takes input)

Then defined a line variable containing a sentence “this is printed in the read function”

Then we made parameters variable, the value of which we got as input from beginning of the function, so when we imported this  readFunction in the application.js file we then passed a value for this parameters variable to hold in it a value which is the sentence “ —this line is added when readFile1 is called—” 

Then we reassigned the parameters variable by adding the two sentences(the line variable holding sentence “this is printed in the read function” and the parameters variable holding sentence “ —this line is added when readFile1 is called—”

Thus this output is achieved

 “this is printed in the read function —this line is added when readFile1 is called—”

We use console.log function to print a message on the screen for debugging and inspection purposes.

Named Imports in node.js

We can use named imports in node.js to only bring in the module’s specific components that we require. We do this by designating them specifically. In our example, the import statement naming readFunction in curly brackets. 

import { readFunction } from ‘./readFile1';

readFunction(“this is 2nd type of import”);

Output – “this is 2nd type of import —this line is added when readFile1 is called—“

In order to import only readFunction2 we do –

import { readFunction2 } from ‘./readFile1';

readFunction2(“this is 2nd type of import”);

Output – “this is 2nd type of import“

Bundled import in node.js

This is the best course of action if we want to have it all and now we will inspect the process of bundled import in node.js. When we use the syntax * as the import statement gives us an object I that has all of our readFile1 module’s exports as appropriately named properties. 

import * as file1 from ‘./readFile1’;

i.readFunction(“ —this line is added when readFile1 is called—”); 

i. readFunction2(“this is 3rd type of import having both modules together the i.readFunction and readFunction2 both together”)

Remember that even if the file being imported is in the same directory as the file you are importing into, you must explicitly identify your import routes as relative paths by using./. Bringing in from unprefixed pathways such as 

import readFunction from ‘./readFile1’;

Or 

import { readFunction } from ‘./readFile1’;

import { readFunction2 } from ‘./readFile1′;

Or

 import * as file1 from ‘./readFile1’;

Notice that before specifying the module named readFile1 we are appending ./ so as to specify that both the file the application and readFile1 are on the same working directory.

Finally here is walkthrough of how to run this simulation

Create a directory, give it whatever name you like, lets for example name it modules in node.js

Then create 2 files named readFile1.js and application.js

Then start a command prompt in windows by searching cmd or terminal in Mac.

Then go to the above stated directory.

Then initialise the npm modules by typing command — “npm init” and press yes to every question or type the details. (note that I am writing the commands in inverted commas, when you type these commands the inverted commas are not needed)

A detail of this can be found here.

Then finally in the terminal or the cmd prompt type the command “node application.js”

Try this file with all the three kinds of import types and see the desired output as specified for each.

Related posts
Realtime and Scalable Web Applications

Node.js Twitter API: 5 Steps to Twittersphere Mastery

Realtime and Scalable Web Applications

Using Variables In Node.js: Quick and brief - Logic Latest

Realtime and Scalable Web Applications

Node.js event loops and callbacks: A brief walkthrough

Realtime and Scalable Web Applications

node.js file system: A detailed walkthrough with codes 2022

Leave a Reply

Your email address will not be published. Required fields are marked *