In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "what is nodejs module". In the operation process of actual cases, many people will encounter such difficulties. Next, let Xiaobian lead you to learn how to deal with these situations! I hope you can read carefully and learn something!
Modules are the basic components of Node.js applications. Files and modules are one-to-one. A Nodejs module is a file, and this file may be JavaScript code, JSON or compiled "C/C++" extensions. Reference modules can be made with the "require ('file path')" statement.
本教程操作环境:windows7系统、nodejs 12.19.0版、Dell G3电脑。
为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统。
模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的。换言之,一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码、JSON 或者编译过的C/C++ 扩展。
对于nodejs来说,一个文件就是一个模块,你可以export接口出去,也可以require别的模块进来。
// module1.jsexports.func1 = function(){ console.log('func1 from module1 called');}
module1把函数func1通过exports对象作为模块的公共访问接口。
//module2.jsvar in_module1 = require('./module1.js');in_module1.func1();exports.func2 = function(){ console.log('func2 from module2 called');}
module2把module1 require进来,这个时候,in_module1就相当于module1的exports对象。当使用in_module1调用func1的时候,相当于通过module1的exports对象调用func1。
同时,module2自己的函数func2也通过模块的exports对象作为module2公共接口。
// module3.jsvar in_module2 = require('./module2.js');in_module2.func2();
同理,module3把module2 require进来,此时in_module2就相当于module2的exports对象。
运行结果如下:
rlan@rlan-LA:~/nodejs/nodetest$ node module2.jsfunc1 from module1 calledrlan@rlan-LA:~/nodejs/nodetest$ node module3.jsfunc1 from module1 calledfunc2 from module2 called
nodejs引入模块不仅仅得到模块的公共接口,同时会把文件里别的语句一并引用进来,比如:
module1.js改为
// module2.jsconsole.log('this is in module2');var in_module1 = require('./module1.js'); in_module1.func1(); exports.func2 = function(){ console.log('func2 from module2 called');}
module2引入了module1的func1函数,同时执行了module1中的打印语句:
rlan@rlan-LA:~/nodejs/nodetest$ node module1.jsthis is in module1rlan@rlan-LA:~/nodejs/nodetest$ node module2.jsthis is in module2 - module2 selfthis is in module1 - require module1func1 from module1 called - module2 self
现在,module2 载入了module1,module3载入了module2,如果module3再载入一次module1会怎么样呢?
// module3.jsvar in_module1 = require('./module1.js');var in_module2 = require('./module2.js');in_module1.func1();in_module2.func2();
这时候,module3首先载入了module1,又载入了module2,module2自己又载入了module1的部分,运行结果为
rlan@rlan-LA:~/nodejs/nodetest$ node module3.jsthis is in module1 - require module1this is in module2 - require module2func1 from module1 called - require module2func1 from module1 called - module3 selffunc2 from module2 called - module3 self
假如把module3的require顺序调整一下:
// module3.jsvar in_module2 = require('./module2.js');var in_module1 = require('./module1.js');in_module1.func1();in_module2.func2();
运行结果为:
rlan@rlan-LA:~/nodejs/nodetest$ node module3.jsthis is in module2 - require module2this is in module1 - require module2func1 from module1 called - require module2func1 from module1 called - module3 selffunc2 from module2 called - module3 self
看起来nodejs用某种机制保证了同一个模块在另一个模块里不会被重复载入,所以
this is in module1
这一行只出现了一次,虽然在module3.js里似乎被载入了两次。
那么,如果循环载入了会发生什么呢?现在我们让module1来require module2:
// module1.jsconsole.log('this is in module1');var in_module2 = require('./module2.js');exports.func1 = function(){ console.log('func1 from module1 called');}// module2.jsconsole.log('this is in module2');var in_module1 = require('./module1.js');in_module1.func1();exports.func2 = function(){ console.log('func2 from module2 called');}
运行结果如下:
rlan@rlan-LA:~/nodejs/nodetest$ node module1.jsthis is in module1this is in module2/home/rlan/nodejs/nodetest/module2.js:4in_module1.func1(); ^TypeError: in_module1.func1 is not a function at Object. (/home/rlan/nodejs/nodetest/module2.js:4:12) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10) at Module.load (module.js:344:32) at Function.Module._load (module.js:301:12) at Module.require (module.js:354:17) at require (internal/module.js:12:17) at Object. (/home/rlan/nodejs/nodetest/module1.js:3:18) at Module._compile (module.js:410:26) at Object.Module._extensions..js (module.js:417:10)rlan@rlan-LA:~/nodejs/nodetest$ node module2.jsthis is in module2this is in module1func1 from module1 called
nodejs似乎阻止了载入自己的行为,运行module2的时候,行为跟module1没有载入module2的结果一样,并没有报错。而在运行module1的时候,当走到module2里面,忽略了require module1的语句之后,module2调用了module1的func1,程序出错。
综上,nodejs里嵌套重复载入模块(或者载入自己)的require语句是不能正确执行的。
"什么是nodejs模块"的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注网站,小编将为大家输出更多高质量的实用文章!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.