The difference between import module require and import in node

tags: node import module

Should I use require or import?

The use of require is very simple, it is equivalent to the portal of module.exports, what is behind module.exports, what is the result of require, objects, numbers, strings, functions... ...And then assign the result of require to a variable, which is equivalent to overlapping the positions of require and module.exports in parallel space.

And theoretically require can be used anywhere in the code, even without assigning a value to a variable before use, such as:

require('./a')(); // module a is a function, execute module a function immediately
var data = require('./a').data; // A module exports an object
var a = require('./a')[0]; // A module exports an array

When you use it, you can completely ignore the concept of modularity and use require, just treat it as a built-in global function of node, and its parameters can even be expressions:

require(process.cwd() + '/a');

But import is different, it is compiled at compile time (require is at runtime), it must be placed at the beginning of the file, and the format used is also definite, so there is no doubt. It does not assign the entire module to a variable after running, but only selects the import interface for compilation, which is much better in performance than require.

From the perspective of understanding, require is an assignment process, and import is a deconstruction process. Of course, require can also deconstruct the result and assign it to a set of variables, but when import encounters default, it is completely identical to require. Different:var $ = require('jquery'); with import $ from 'jquery'They are two completely different concepts.

There is no answer to the question "require or import instead?" because this question is currently unanswerable, because all engines have not yet implemented import. We Using babel in node to support ES6, it just transcodes ES6 to ES5 and then executes, the import syntax will be transcoded to require. This is why module.exports is used when exporting modules, and import is still effective when importing modules, because in essence, import will be transcoded to require for execution.

However, we must know this truth, ES7 will be released soon, and js engines will implement the ES6 standard as soon as possible. If an engine cannot even achieve the standard, it will be eliminated ,ES6 is a matter of time. If you are still deploying require in your code, you must upgrade your code when ES6 is supported by the engine, and if you start deploying import now, you may only need to do very few changes in the future.

Intelligent Recommendation

Difference between require and import

#require and import difference The most important idea in node programming is modularity, and import and require are both used by modularity. In ES6, use export to export the interface and import to i...

The difference between require and import in JS

The difference between require and import in JS Both of these are used for JS modular programming. Follow the specification requireIs the introduction method of AMD specification importIs a grammar st...

What is the difference between require and import?

What is the difference between require and import? Different specifications to follow 1.require/exports is part of CommonJS 2.import/export is the new ES6 specification Different time of appearance Co...

Difference between require and import in JavaScript

When modifying vue&react and webpack, I often see require and jload in js file, both of which are used for JS modular programming (CSS is @import). Let's look at the difference between them. where...

Analysis of the difference between import and require

1. require is the AMD specification, and import is the ES6 module. 2. require similar way of writing It seems to only introduce a, b, c, actually introduce the whole module, and then assign the corres...

More Recommendation

[js] difference between require and import

Article directory The difference between `require` and `import` origin Export command / import command CommonJS es6 Static optimization requirewithimportDifference origin Before the birth of es6, js h...

The difference between require and import in vue

The difference between require and import Before es6, js has not had its own module syntax. In order to solve this problem, there is a requirement for require.js. After the release of es6, js introduc...

The difference between the import and require the webpack

Foreword Some time ago wrote an articleimport and require Depth - depth from shallow thinking to bring NodeThe articles comeimportis based onrequireImplementation, and implementation is different in d...

(Transfer) The difference between import and require

Before ES6, there was a bill to load JS modules, the most important being CommonJS and AMD specifications. The former CommonJS is mainly used in servers to achieve synchronous loading, such as nodejs....

The difference between modular require and import

When the front-end application becomes more and more complex, we want to divide the code into different modules for easy reuse and on-demand loading. Require and import are statements that introduce m...

Copyright  DMCA © 2018-2026 - All Rights Reserved - www.programmersought.com  User Notice

Top