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.exportsload 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 requireeach 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 scopemodule 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 functionmodule 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 Supermanin 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.
网友评论