美文网首页
CommonJS module & module wrapper

CommonJS module & module wrapper

作者: Time_Notes | 来源:发表于2024-04-30 04:07 被阅读0次
three types of modules: local/built-in/third-party

in nodejs, each file is a module, but is isolated by default

how to ask nodejs not only execute index.js but also other files? This is where the commonjs module format comes into the picture.

a standard: how a module should be structured and shared commonjs standard

./  dot slash refers to the same folder

require function loads the add module into index.js

local module uses require and module.exports

load module using the require function

by requiring a module, we are basically asking v8 to execute the code in that module

to reuse a block of code is to expose certain functionality that can be consumed by external files or apps. we can to that using the special module.exports object (available in nodejs)

the value of this module.exports object is what the require function returns for the corresponding module, this allows us to store it in a constant

module.exports = add

const addFn = require("./add")

this is called a default export where you can refer to the returned value using any name

commonjs module format for exporting and importing functionality that nodejs adheres to

exports/require require

each module in nodejs has its own scope, the way nodejs achieve that is with the immediately invoked function expression (IIFE) in JS, each function gets its own private scope

(function(){})()

wrap it with parentheses to convert it into a function expression

add parentheses at the end to immediately invoke it

under the hood nodejs uses this IIFE pattern

IIFE pattern with function wrapper module scope
module wrapper 5 parameters: exports, require, module, __filename, __dirname

by wrapping each module code in this IIFE with 5 parameters, nodejs provides few global looking variables that are actually specific to the module, this is how we get access to require and module.exports within a file, they are injected during execution by nodejs

local variables in debug panel: convenience variables

__dirname: directory name or folder name of current module

__filename: file name of current module

exports

require: import a module by path

module: reference to the current module

this

__dirname

//module wrapper function

(function(exports,require,module,__filename,__dirname){

    const obj = require('./')

    module.exports = log;  

    //or module.exports.log = log;

})

每个module都是一个对象 module wrapper function

module caching

in Nodejs when we require a new module, it is loaded and cached for subsequent loading

creating a new instance of superHero newSuperHero name is also Superman

in line 1, the superhero module is loaded and cached (remenbered), so next time we require the same module on line 9, nodejs will remember has already been loaded before, reuse that instead of doing the additional work of loading it brand new, in this case it is going to load the same object as line one. Since objects are passed by reference, we get the same object whose name has been modified to Superman. This is how module caching works in nodejs, in real world application, the same module is typically imported in several different files and caching is what helps with performance.

require.cache

on line 6, execution does not bring us back to the superHero module, it goes straight to the next line. The code inside nodeJS is not parsed again, the same object is returned.

How do we deal with scenarios where we need to create separate instances? Instead of exporting an instance, we export the class itself.

Instead of exporting an instance, export the class itself.

相关文章

网友评论

      本文标题:CommonJS module & module wrapper

      本文链接:https://www.haomeiwen.com/subject/fzrfictx.html