Import/Export Vs Require/Module.exports

Import/Export

1) Named exports (several per module)

//------ lib.js ------
export const sqrt = Math.sqrt;
export function square(x) {
return x * x;
}
export function diag(x, y) {
return sqrt(square(x) + square(y));
}

How to use lib.js?

//------ main.js ------
import { square, diag } from 'lib';
console.log(square(11)); // 121
console.log(diag(4, 3)); // 5

Or

//------ main.js ------
import * as lib from 'lib';
console.log(lib.square(11)); // 121
console.log(lib.diag(4, 3)); // 5

Note: You can also do export in the following ways as well.

// module "my-module.js"
function cube(x) {
return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
var graph = {
options: {
color:'white',
thickness:'2px'
},
draw: function() {
console.log('From graph draw function');
}
}
export { cube, foo, graph };

2) Using the default export

If we want to export a single value or to have a fallback value for our module, we could use a default export:

// module "my-module.js"
export default function cube(x) {
return x * x * x;
}

Then, in another script, it will be straightforward to import the default export:

import cube from './my-module.js';
console.log(cube(3)); // 27

Note:
1) It is not possible to use , or with .
2) You can’t have more than one default export.

require and module.exports

Best blog to read about in detail:
https://medium.freecodecamp.org/requiring-modules-in-node-js-everything-you-need-to-know-e7fbd119be8

Some important points:
Requiring a folder
Modules don’t have to be files. We can also create a folder under and place an file in there. The same line will use that folder’s file:
Note: An file will be used by default when we require a folder, but we can control what file name to start with under the folder using the property in . For example, to make the line resolve to a different file under the folder, all we need to do is add a file in there and specify which file should be used to resolve this folder:

require.resolve

If you want to only resolve the module and not execute it, you can use the function. This behaves exactly the same as the main function, but does not load the file. It will still throw an error if the file does not exist and it will return the full path to the file when found.

Note :This can be used, for example, to check whether an optional package is installed or not and only use it when it’s available.

exports, module.exports, and synchronous loading of modules

A module object has ‘exports’ property.We can add any attribute to this special exports object. For example, let’s export an id attribute for and :

// Add the following line at the top of lib/util.js
exports.id = 'lib/util';
// Add the following line at the top of index.js
exports.id = 'index';

The object in every module is what the function returns when we require that module.

exports.id = 42; // This is ok.exports = { id: 42 }; // This will not work.module.exports = { id: 42 }; // This is ok.

Note: All modules will be cached.
Node caches the first call and does not load the file on the second call.

We can see this cache by printing after the first require. The cache registry is simply an object that has a property for every required module. Those properties values are the objects used for each module. We can simply delete a property from that object to invalidate that cache. If we do that, Node will re-load the module to re-cache it.

Suffering from Knowledge Quest

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store