Difference between import http = require('http'); and import * as http from 'http';?

In the first form, you create an http object in your code (totally clean), then, the interpreter will look for each possible import in http module and append it, one by one, to the http object in your code, this is a little slower (not much) than the second form where you are getting the module.exports object defined in the http module, then copying this reference to a new http object in your code, this is object in a node special function with a particular context, not only an object created in your code with the contents of the module.


While in a node environment where you've configured the module type to be common JS the output will be the same. Other module frameworks will utilize different syntax and by using the first approach you have the flexibility to change that at will.

Also of note about the import * as http from 'http'; approach is that it is the ES6 module import syntax, so once you are in an environment that fully supports ES6 your imports will just work.


import http = require('http') //Common JS

This is Common JS modules. Before version 12.2, this was the only way to use modules in node JS.

import * as http from 'http'; //ES 6

This is ES6 module. In ECMAScript 6 standards, modules are natively supported by Javascript. Node JS implemented this feature in version 12.2.

Out of these two, I always prefer ES6 module because it is part of javascript implementation. ES6 module is also supported by browser. But Common JS is not supported by browser since it is synchronous. AMD module was used in browser before ES 6 because it is asynchronous unlike CommonJS