Both require and import are used to import modules.
Import is the grammar standard in es6, and its corresponding is export;
And require is just a private global method of node, based on the AMD specification, corresponding to exports. Node.js is based on the CommonJS specification. AMD is very similar to CommonJS in its use.
Module loading time is different:
Require is runtime loading, and import is compiled at compile time. Therefore, require can be written anywhere in the code, and import needs to be written at the very top of the file.
The essence of the module:
requireIs the assignment process, it is equivalentmodule.exportsThe portal, what is the content behind module.exports, what is the result of require, objects, numbers, strings, functions... then assign the result of require to a variable.
The method of use is as follows:
Module output:
a.js:
module.exports={
a:function(){
......
},
Url: "I am a bunch of links"
}
Module import:
b.js:
var obj = require('./a);
obj.a();
console.log(obj.url);
If you need to use the require to load the es6 default export (export default mycomponent) module, you need to use require ('...').default。The main reason is: when processing the es6 module in node, the format of the module that is loaded and exported is:
{
default:mycomponent
}
importIs a deconstruction process, the module of ES6 is not an object, but aexport The code that the command outputs. We use babel to support ES6 in node, and only transcode ES6 to ES5 and execute it. Now we use import in node and will be transcoded into require.
With import, you need to know about export.
export Statements are used to derive functions, objects, or raw values from a module so that other programs can use them through the import statement.
The exported modules are in strict mode regardless of whether strict mode is declared.
There are two different export methods, named export and default export. Multiple named exports can be defined in each module, but only one default export is allowed.
Named export: Use the same name when importing.
In a.js:
/ / Export the method declared before
export { myFunction };
// Export a constant
Export const foo = Math.sqrt(9);//returns the square root of a number
//Import in other scripts:
In b.js:
import {myFunction,foo} from "./a";
console.log(myFunction());
Console.log(foo);//output 3
Default export: When importing the default export, the import name does not need to match the name at the time of export.
In a.js:
/ / Export function
export default function() {}
/ / Export class
export default class A{......}
Note: The above two default exports cannot appear in one file at the same time.
Import in b.js:
import funM from './a';
import A from './a';
Note that you cannot usevar、letOrconstUsed to export default valuesexport default。
Therefore, to export a variable, write as follows:
Var a=1;//Declare the variable first
export default a;
The following way of writing is wrong: export default var a=1.
Essentially, the export default of the a.js file outputs a variable called default, and the system allows you to take any name for it.
So, about export default:
export default function (){}
Equivalent to:
var function a(){...}
export {a as default}
When importing:
import a from './x';
Equivalent to:
import {default as a} from './x';
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...
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...
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....
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...
First we have to understand the basic syntax of Require and Import: REQUIRE's basic syntax: Define module.export in the exported file, the type of object to export is not limited (which can be any typ...
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, str...
Both require and import are used for js modularization. First, require Require is the specification of commonjs. The node application is composed of modules and complies with the specification of comm...
Require/exports appears first. It is produced in the specifications drafted by the developers of the js community and is widely recognized or widely used. Import/export is ES6. CommonJS is a specifica...
import is a reference require a copy In theory, the value of reference require changes will no longer be referenced files inside the affected, and this indeed is the case, That way the value of...
require () / import command / import () function is the difference between: require a loading operation, that is dynamically loaded. The import command is a static load, load at compile time. import (...