-1

Why in JS,
React handles importing like this

import SomeThing from '../../components/SomeComponent/SomeThing';

while
Node handles importing like this

const someThing = require('../someWhere/someThing');

Is it purely a convention?
If so, are they exchangeable?
If not, why is it important to be done so?
Is there any difference between them behind the scene, e.g. performance?

Matin Sasan
  • 1,835
  • 1
  • 13
  • 26
  • 1
    This is solve your doubt https://facebook.github.io/create-react-app/docs/importing-a-component – Prabhat Mishra May 17 '19 at 17:45
  • 1
    `import` is ES6 and is not supported in node (*yet*) without additional workaround. [How can I use an es6 import in node?](https://stackoverflow.com/questions/45854169/how-can-i-use-an-es6-import-in-node) – crashmstr May 17 '19 at 17:54
  • @RandyCasburn thanks to your link, I starred two questions to study them. A link in a comment in your link provided detailed answers: https://stackoverflow.com/questions/31354559/using-node-js-require-vs-es6-import-export – Matin Sasan May 17 '19 at 17:59

1 Answers1

-1

import is ES6 that is "dumbed" down by babel to compile in all browsers require is ES5. React is using import because behind the scenes it's using babel as compiler. They are not exchangeable, in the sense that in React you can use require and import, but in node natively you can just use require.

KT-mongo
  • 2,044
  • 4
  • 18
  • 28
  • I think "dumbed" down is not a good phrase (`require` is neither smarter nor dumber than `import`), plus you can, with the right command line options, use `import` in node. – crashmstr May 17 '19 at 17:56
  • Well you can do everything with the right command, can't you? But this is an answer to a question not made. I interpret es6 => es5 as "dumbed" down, you may interpret the contents of " " as you wish. – KT-mongo May 17 '19 at 18:00
  • `require()` is not related to the ES5 spec. `import` can also be used in NodeJS "natively", although still experimental – Jonas Wilms May 17 '19 at 18:03