why node uses require not import?

Holly

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.

axm__

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Node Require and Import

Why use import and require() in Node.js code?

Node, formidable - Why does require work but import does not?

Import works, but why is require not working?

Why does AFrame require Node?

How to use import instead of require with node js?

translate require() syntax to import syntax node js

cannot import or require node modules (using npm)

Require, Import, Require Import

Why does "require" compile but "import" does not?

Why does calling a method not require an import of the class?

Why does shapely wkt require separate import?

Does require in node uses eval to run a code in another file

In Node, why does 'require' assignment sometimes require curly brackets?

Nodejs require()(require()) to import

Angular 5 CLI import/require custom node module

When to use "import" and when to use "require" in Node.JS

Unexpected identifier when using import abc = require('node-module')

How to resolve require vs. import in a Node.js script

import css file from node_modules using require()

Node.js require vs React.js import

Why does Python's __import__ require fromlist?

Why does require not behave the same as import when same structure is given

why is requirejs on node not loading the require specified before continuing

Why doesn't node.js load the module I require?

Why does the Openstack Autopilot require two disks on each node?

Why does node-gyp (and other stuff) require Visual Studio?

Why can't I require modules in Node.js shell?

Why _ as variable name in node REPL mode to require failed

TOP Ranking

HotTag

Archive