Difference between node.js require and ES6 import and export

09 Mar, 2022
Node.js follows the commonJS module system, and it require to include modules that exist in separate files and for that purpose it has methods like “require” and “ES6 import and export” are available in node.js.
Require: It is the builtin function and it is the easiest way to include modules that exist in separate files. The basic functionality of require is that it reads a JavaScript file, executes the file, and then proceeds to return the export object. It not only allows you to add built-in core Node modules but also community-based modules (node_modules), and local modules in the desired program. There are various inbuilt modules in a node like – HTTP module, URL module, query string module, path module and a lot more.
Syntax:
To including inbuilt module is as follows:const express = require('express');
const express = require('express');
To including local module is as follows. For an instance, you require ‘abc’ module, without specifying a path.require('abc');
require('abc');
Example: Node will look for abc.js in all the paths specified by module.paths in order.
Input:require('abc');
require('abc');
Output:If node can’t find it:Error: Cannot find module 'abc'    at Function.Module._resolveFilename (module.js:470:15)    at Function.Module._load (module.js:418:25)    at Module.require (module.js:498:17)    at require (internal/module.js:20:19)    at repl:1:1    at ContextifyScript.Script.runInThisContext (vm.js:23:33)    at REPLServer.defaultEval (repl.js:336:29)    at bound (domain.js:280:14)    at REPLServer.runBound [as eval] (domain.js:293:12)    at REPLServer.onLine (repl.js:533:10)If node find it:// It is the content of the fileGeeksforgeeks example for require
If node can’t find it:Error: Cannot find module 'abc'    at Function.Module._resolveFilename (module.js:470:15)    at Function.Module._load (module.js:418:25)    at Module.require (module.js:498:17)    at require (internal/module.js:20:19)    at repl:1:1    at ContextifyScript.Script.runInThisContext (vm.js:23:33)    at REPLServer.defaultEval (repl.js:336:29)    at bound (domain.js:280:14)    at REPLServer.runBound [as eval] (domain.js:293:12)    at REPLServer.onLine (repl.js:533:10)
Error: Cannot find module 'abc'    at Function.Module._resolveFilename (module.js:470:15)    at Function.Module._load (module.js:418:25)    at Module.require (module.js:498:17)    at require (internal/module.js:20:19)    at repl:1:1    at ContextifyScript.Script.runInThisContext (vm.js:23:33)    at REPLServer.defaultEval (repl.js:336:29)    at bound (domain.js:280:14)    at REPLServer.runBound [as eval] (domain.js:293:12)    at REPLServer.onLine (repl.js:533:10)
If node find it:// It is the content of the fileGeeksforgeeks example for require
// It is the content of the fileGeeksforgeeks example for require
ES6 Import & Export: These statements are used to refer to an ES module. Other file types can’t be imported with these statements. They are permitted only in ES modules and the specifier of this statement can either be a URL-style relative path or a package name.
Whereas Export statements allow the user to export his created objects and methods to other programs. For instance, if you assign a string literal then it will expose that string literal as a module.
Syntax:
For importing file.// Importing submodule from 
// 'es-module-package/private-module.js';
import './private-module.js';
// Importing submodule from 
// 'es-module-package/private-module.js';
import './private-module.js';
For exporting file.module.exports = 'A Computer Science Portal';
module.exports = 'A Computer Science Portal';
Example: Create two JS file one is for importing and another one is for exporting or you can use any module to import so export one will not be required.
Export File name Message.js// Exporting modulemodule.exports = 'Hello Geek';ImportFile name Display.js// Importing modulevar msg = import('./Message.js');console.log(msg);</li>      <li><strong>Output:</strong><div class="noIdeBtnDiv">Hello Geek
// Exporting modulemodule.exports = 'Hello Geek';
ImportFile name Display.js
// Importing modulevar msg = import('./Message.js');console.log(msg);</li>      <li><strong>Output:</strong><div class="noIdeBtnDiv">Hello Geek
Difference between node.js require and ES6 import and export: Although require function and ES6 import and export share a lot in common and seem to perform the same function in code but they are different in many ways.
NOTE: You must note that you can’t use require and import at the same time in your node program and it is more preferred to use require instead of import as you are required to use the experimental module flag feature to run import program.
chusapkrerkkiat
kothavvsaakash
ES6
Node.js-Misc
Picked
JavaScript
Node.js
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.