[js] difference between require and import

tags: JS  Modular  CommonJs  es6

requirewithimportDifference

origin

Before the birth of es6, js has no module system, even css has@importIt is undoubtedly a hindrance to the development of complex large-scale projects. Es6 was officially released in June 2015, so es6 also has a separate name es2015. Before the official did not propose es6, the community gave a solution to the module problem, that is node.js. Introduced to this place, in factrequirewithimportThe origins have emerged, the keywords used in the module solution in es6 areimportAnd the keyword used by the solution provided by node.js isrequire

Node.js was released by Ryan Dahl in 2009. The modularity provided by node.js is also called CommonJS, but node.js is mainly used for server-side development, but with the development of the times, some front-end frameworks now rely on node.js.

Export command / import command

Want to userequireorimportFirst of all, we must know how to use the two methods of exporting. From now on, the following two solutions are distinguished by CommonJS and es6.

CommonJS

  • Export variables, methods, objects
// testcommonjs.js
exports.a = 123;
exports.b = 'hello';
exports.c = function(){
  console.log('ccc')
}
exports.d = {
  foo: 'bar'
}

//Equivalent => 
module.exports.a = 123;
module.exports.b = 'hello';

//Equivalent => 
module.exports = {
  a:123,
  b:'hello',
  c:function(){
    console.log('ccc')
  },
  d:{
    foo:bar
  }
}

It is recommended to use module.exports, using exports alone will produce unexpected errors in some cases, which is not the focus of this article.

  • Introduction and use
//index.js
var test = require('./testcommonjs')
console.log(test.a)
console.log(test.b)
test.c()
console.log(test.d.foo)

es6

  • Export variables, methods, objects (only some commonly used methods are listed here, please refer to the specific usage methods)Getting started with es6)
//testesmodule.js
export var a = 123
export var b = 'hello'
export function c(){
  console.log('ccc')
}
export var d = {
  foo: 'bar'
}
  • Introduction and use
import { a,b,c,d } from './testesmodule'

console.log(a)
console.log(b)
c()
console.log(d.foo)

//Equivalent => 
import * as test form './testesmodule'
console.log(test.a)
console.log(test.b)
test.c()
console.log(test.d.foo)

Static optimization

This is a concept learned from the document of the great god of Yiyi. He uses the core module of node in the document.fsGive an example

  • es6
// ES6 module
import { stat, exists, readFile } from 'fs';

Note: Do not try without configurationbabelIn the project to test the above code, because node still does not support the modular syntax of es6 (the current node version of the author: v10.15.0), although it has implemented most of the es6 features.

  • CommonJS
// CommonJS module
let { stat, exists, readFile } = require('fs');

It looks like this,fsThe way the module is introduced seems to be only a syntactic difference between es6 and CommonJS, but in fact, the above code is handled in CommonJS like this.

let _fs = require('fs');
let stat = _fs.stat;
let exists = _fs.exists;
let readfile = _fs.readfile;

In other words, in order to usefsmiddlestatexistsreadFileMethod, in code execution makes it necessary to putfsThe entire module is loaded in, generating an object_fsAnd then read three methods from this object, this kind of loading is called "runtime loading", because only the runtime can get this object.

The design of the es6 module is as static as possible, so that the dependencies of the module and the variables of the input and output can be determined at compile time. From this point of view, the modular efficiency of es6 is much higher than that of CommonJS. also listed some other advantages of static loading in the document, but this dish b can not read ╮(╯▽╰)╭

New thinkingfsIs the core module of node.js, why the modular syntax of es6 can take effect, through file positioning in ws, found that the core module code of node.js is written with ts, but because of the ts syntax is not familiar, can not understand How ts can be compatible with both CommonJS and es6.

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

require and import difference in JS

Both are aimed at JS modular programming. Follow the norms requireAMD is introducing standardized way importThe syntax is a standard es6, if you want a compatible browser then be converted into the sy...

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

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

The difference between the Require and Import of Vue

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

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

Top