18

I'm learning node.js and am wondering why it uses the require syntax rather than the import syntax which React uses.

i.e.

const Validator = require("validator");

VS

import Validator from "validator";

I believed import is es6 but I don't think that explains why it's not used in node.

3
  • 1
    "I don't think that explains why it's not used in node" - of course it does: Node.js if far older than ES6 Commented Sep 29, 2018 at 11:11
  • Have a look at the history of commonjs modules - the discussion was around 2010, five years before ES6 Commented Sep 29, 2018 at 11:20
  • 1
    because I often see arrow functions and other es6 features used in node I thought import would have been the same, it's available in chrome & V8 too. But I see why it's not supported from the link @axm__ shared in his answer Commented Sep 29, 2018 at 11:24

4 Answers 4

9

the import and default are newer ES6 features, not yet used by node. Node is actually already implementing the new features as experiment though: with the --experimental-modules flag and only for files saved with .mjs extension.

Transpilers like babel make it possible to write modern, spec approved and /or experimental ECMAScript. In an ecosystem of bundlers like Webpack with transpilers like babel, it becomes easy to write maintainable, future-proof javascript, while the code remains widely suported because it's transformed to commonjs (the format you see recognizable byrequire (the old-school import) and module.exports (the old-school export).

Sign up to request clarification or add additional context in comments.

1 Comment

Note that Bable and Typescript's implementation of the import system is not spec approved. Specifically the spec requires that the imported module's file name match the string you pass to the import statement. The whole thing with the .mjs type is due to the impossibility of making the ES6 module system work with Commonjs (node's module system)
1

Probably for historical reasons. node.js and chrome (v8 engine) are older than ES6 standard.

On the other hand, see: How can I use an es6 import in node?

You may use import, too.

Comments

1

I believed import is es6 but I don't think that explains why it's not used in node.

Just like the way NodeJS implements their entire library which tons of asynchronous functions which only support callback-based approach. Thinking this way and you'll realize that, sooner or later, the NodeJS framework will definitely support the import syntax and upgrade all of those asynchronous functions to support promise-based.

Comments

1

In Node.js, require is used to load modules, while import is used in ECMAScript modules (ESM modules) for loading other modules, either statically or dynamically.

The major difference between require and import is that require will automatically scan node_modules to find modules, but import, which comes from ES6, won’t.

Most people use Babel to compile import and export, which makes import act the same as require.

  1. require is more of dynamic analysis, and import is more of static analysis.
  2. require throws error at runtime, and import throws error while parsing.
  3. require is nonlexical and import is lexical.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.