top of page
Writer's pictureThe Tech Platform

JavaScript Require() vs Import()



JavaScript Require()

In NodeJS, require() is a built-in function to include external modules that exist in separate files. require() statement basically reads a JavaScript file, executes it, and then proceeds to return the export object. require() statement not only allows to add built-in core NodeJS modules but also community-based and local modules.


Syntax:

To include a module, the require() function is used with the name of the module:

var myVar = require('http'); //to use built-in modules
Var myVar2 = require('./myLocaModule') to use local modules

Require Features:

  • You will have dynamic loading wherever the loaded module name is not predefined /static or wherever you do not absolutely load a module, providing it’s “truly required” (depending on bound code flow).

  • Loading is synchronous. Meaning if you have got multiple requires, they’re loaded and processed one by one.

  • You will have dynamic loading wherever the loaded module name is not predefined /static or wherever you do not absolutely load a module, providing it’s “truly required” (depending on bound code flow).

  • Loading is synchronous. Meaning if you have got multiple requires, they’re loaded and processed one by one.

  • The requirement is not customarily based mostly. It’s is extremely unlikely to become customary currently that ES6 modules exist.

The actual loading of any module using require() happens in five steps.

  1. Resolution

  2. Loading

  3. Wrapping

  4. Evaluation

  5. Caching



JavaScript Import()

import() & export() statements are used to refer to an ES module. Other modules with file types such as .json cannot be imported with these statements. They are permitted to be used only in ES modules and the specifier of this statement can either be a URL-style relative path or a package name. Also, the import statement cannot be used in embedded scripts unless such script has a type="module". A dynamic import can be used for scripts whose type is not “module”


Syntax:

var myVac = import("module-name");

Import features:

  • You will use named imports to by selection load solely the items you would like. Which will save memory?

  • Import is asynchronous (and in the current ES6 Module Loader, it, of course, is) and may perform a touch higher.

  • You will use named imports to by selection load solely the items you would like. Which will save memory?

  • Import is asynchronous (and in the current ES6 Module Loader, it, of course, is) and may perform a touch higher.

  • Imports don’t seem to be obtainable in Node because of version 6.

  • However, it’d are available in future versions. You’ll use it these days, using transpilers similar to Traceur Compiler, Babel or Rollup.



Difference Between Require and Import


Require Import

Syntax:

var dep = require(“dep”);

console.log(dep.bar);

dep.foo();

Syntax:

import {foo, bar} from “dep”;

console.log(bar);

foo();

Require is more Dynamic analysis

Import is more static analysis

Require throws error at runtime

Import throws error while parsing

Require is Nonlexical

Import is Lexical

Requires to stay where they have put the file

Imports get sorted to the top of the file.

Require can be used inline, conditionally,

Import is always run at the very beginning of the file and can’t be run conditionally



The Tech Platform

0 comments

Recent Posts

See All

Comments


bottom of page