3

Right now I'm importing modules into node project as

import * as name from "moduleName";

Im not doing it as

var name = require("moduleName");

as we used to be in the node project, earlier

my question is there difference in the writing a module when we import using require or import, are modules written internally are same just we are importing in different way or its something internal that forces us to use require or import when importing

and what is the difference between require and import(es6)

Thanks!

blackHawk
  • 6,047
  • 13
  • 57
  • 100
  • 3
    Check [this](http://stackoverflow.com/questions/31354559/using-node-js-require-vs-es6-import-export) and [this](http://researchhubs.com/post/computing/javascript/nodejs-require-vs-es6-import-export.html) – Thaadikkaaran Dec 08 '16 at 06:03
  • http://stackoverflow.com/questions/31354559/using-node-js-require-vs-es6-import-export – Adiii Dec 08 '16 at 06:08
  • what I understood with both links, that for import module using require we need to export differently than importing ES6, it means we need some modifications in module for importing – blackHawk Dec 08 '16 at 06:51
  • So we can first see how module is exported and than import the way it is or we can use both imports no matter what implementation is – blackHawk Dec 08 '16 at 06:55

1 Answers1

1
  1. import runs at the beginning of the file and it would be already loaded before the code itself runs.
  2. require on the other hand is run inline and can be inserted within the code conditionally.
Remya CV
  • 144
  • 6
  • what do you mean by run inline and can be inserted within the code conditionally – blackHawk Dec 08 '16 at 06:53
  • var module1 = require("module1"); is written within the block of code such as an if /else and not at the top like import statements. Therefore the loading order of modules differ. – Remya CV Dec 08 '16 at 07:01
  • right, then there is difference between module implementation for import using require and es6 import? or no matter whatever the implementation but we can import them using es6 – blackHawk Dec 08 '16 at 07:09