JS Reference
Service Workers
Web Components
ESModule Tips
NodeJS Tips
Javascript NodeJS

NodeJS - import scripts using CommonJS

Old way of importing scripts

How to import Javascript files using CommonJS

Call a function from a library javascript file

Info

Import the Javascript files old way using CommonJS method.

Sample Code

script.js
/lib/welcome.js
package.json
Copy
/* ==========================================
* Common JS import using require
* No .js extension required
* package.json - "type":"commonjs"
* Import by side effects - runs function calls from imported javascript file ""
========================================== */

const welcome = require('./lib/welcome');

console.log("mainfile: First Log Message");

console.log("mainfile: imported function: ", welcome);

welcome();
console.log("Library file: Welcome: Global log function");
                  
function welcome(){
    console.log("Library file: Welcome function log message")
}

module.exports=welcome;
{
    "name"          : "1Import Script - using CommonJS",
    "version"       : "1.0.0",
    "description"   : "",
    "main"          : "script.js",
    "//type"        : "module",
    "type"          : "commonjs",
    "scripts"       : {
                        "test": "echo \"Error: no test specified\" && exit 1"
                      },
    "author"        : "",
    "license"       : "ISC"
}

Sample Output

image description

Import multiple functions and variables using CommonJS

Call a function from a library javascript file

Info

Import the Javascript files old way using CommonJS method.

Sample Code

script.js
/lib/welcome.js
package.json
Copy
/* ==========================================
* Common JS import using require
* No .js extension required
* package.json - "type":"commonjs"
* Import by side effects - runs function calls from imported javascript file ""
========================================== */

//One way of importing the functions
const { welcome, welcomeAgain } = require('./lib/welcome');

//Second way of importing functions using object variable 
const welcomeobj = require('./lib/welcome');

console.log("Welcome to New world of Backend with NodeJS");

console.log("import info: ", welcome);

//Invoke the functions using function variables directly
welcome();
welcomeAgain();

//Invoke the functions using imported object
welcomeobj.welcome();
welcomeobj.welcomeAgain();
console.log("new file");

function welcome() {
    console.log("Welcome and Hello from another file");
}
function welcomeAgain() {
    console.log("Welcome Again and Hello Again from another file");
}

module.exports = { welcome, welcomeAgain };
{
    "name"          : "1Import Script - using CommonJS",
    "version"       : "1.0.0",
    "description"   : "",
    "main"          : "script.js",
    "//type"        : "module",
    "type"          : "commonjs",
    "scripts"       : {
                        "test": "echo \"Error: no test specified\" && exit 1"
                      },
    "author"        : "",
    "license"       : "ISC"
}

Sample Output

image description