CommonJS specification in Node

CommonJS is a big specification Node and browser just borrows some of its essence

Here are the results of running the Node environment:

# a.js

console.log('I am a. s')

Copy the code

Case without reference:

# b.js

const lib = require('./a')
console.log('I am b.j s')

# b.j s operationI'm A.JS. I'm B.JSCopy the code

Where there are citations:

# b.js

const lib = require('./a')
console.log('I am b.j s',lib)

# b.j s operationI am A.js I am B.js {} // get an empty objectCopy the code

There areexportsDefines the output of the module through exports:

# a.js

exports.hello='world! '// Here you can mount legal data types, objects, strings, functions, etc. Console. log('I am a. s')

Copy the code
# b.js

const lib = require('./a')
console.log('I am b.j s',lib)

# b.j s operationI'm A.js. I'm B.js. {hello:'world! '} // Get an object 'key' which is the name of the property mounted on 'exports'Copy the code

Is the reference of B. js require the same reference as that of A. js exports?

# a.js

exports.hello='world! '
console.log('I am a. s')
setTimeout(()=>{
  console.log(exports)
},1000)

Copy the code
# b.js

const lib = require('./a')
console.log('I am b.j s',lib)
lib.addNewProperty = 'I am a new property added to B.js'

# b.j s operationI'm A.js. I'm B.js. {hello:'world! ' }

Exports exports the same reference as require
{ hello: 'world! ', addNewProperty: 'I am a new property added to B.js' } 

Copy the code

What if you export a function directly? exports.function?

a.js

exports.hello='world! '

console.log('I am a. s')

module.exports = function test(){
  console.log('I'm function derived from A.js')}Copy the code
b.js

const lib = require('./a')
console.log('I am b.j s',lib)
lib.addNewProperty = 'I am a new property added to B.js'
console.log(lib.hello)
console.log(lib)

# b.j s operationI'm A.JS. I'm B.JSfunction test(){
  console.log('I'm function derived from A.js'} undefined // addNewProperty // module.exports {[Function: undefined // addNewProperty // module.exports]test] addNewProperty: 'I am a new property added to B.js'} // module.exports overrides the contents of exportsCopy the code

ES6 modules and CommonJS in Node have their own loading schemes.

The CommonJS module’s output is defined in the module.exports property. The import command for Node loads the CommonJS module and Node automatically treats the module.exports property as the module’s default output, equivalent to export default XXX

The front end can use Webpack to write code using CommonJS specifications, Webpack will analyze all CommonJS and generate a big one Js :(function(modeule,exports,__webpack_require__){file contents}}),./xxx.js:(function(modeule,exports){file inside In this form, a scope is created for each file.

  • CommonJS
    // a.js
    module.exports = {
        foo: 'hello',
        bar: 'world'}; / / is equivalent toexport default {
        foo: 'hello',
        bar: 'world'
    };
    Copy the code

conclusion

  • changerequireThe original object is also affected (value reference)
  • exportsrequirejsThe default is an empty object that can have functions inside it
  • exportsthroughexports.xxExport objects, all exports are mountedexportsOn this object,requireThe input defaults to an empty object, defining the output of an object in this way
  • modules.exportsYou can derive a function directly
  • exportsandmodule.exportsCoexisting only exportsmodule.exportsThe content will be coveredexports
  • importThe output is worth referencing (copy the address)