网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

什么是nodejs模块

本篇内容介绍了“什么是nodejs模块”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、网页空间、营销软件、网站建设、禹王台网站维护、网站推广。

模块是Node.js应用程序的基本组成部分,文件和模块是一一对应的,一个Nodejs模块就是一个文件,而这个文件可能是JavaScript代码、JSON或者编译过的“C/C++”扩展,引用模块可用“require('文件路径')”语句。

什么是nodejs模块

本教程操作环境:windows7系统、nodejs 12.19.0版、Dell G3电脑。

为了让Node.js的文件可以相互调用,Node.js提供了一个简单的模块系统。

模块是Node.js 应用程序的基本组成部分,文件和模块是一一对应的。换言之,一个 Node.js 文件就是一个模块,这个文件可能是JavaScript 代码、JSON 或者编译过的C/C++ 扩展。

对于nodejs来说,一个文件就是一个模块,你可以export接口出去,也可以require别的模块进来。

// module1.js
exports.func1 = function(){
        console.log('func1 from module1 called');
}

module1把函数func1通过exports对象作为模块的公共访问接口。

//module2.js
var 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.js
var in_module2 = require('./module2.js');
in_module2.func2();

同理,module3把module2 require进来,此时in_module2就相当于module2的exports对象。

运行结果如下:

rlan@rlan-LA:~/nodejs/nodetest$ node module2.js
func1 from module1 called
rlan@rlan-LA:~/nodejs/nodetest$ node module3.js
func1 from module1 called
func2 from module2 called

nodejs引入模块不仅仅得到模块的公共接口,同时会把文件里别的语句一并引用进来,比如:

module1.js改为

// module2.js
console.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.js
this is in module1
rlan@rlan-LA:~/nodejs/nodetest$ node module2.js
this is in module2         - module2 self
this is in module1         - require module1
func1 from module1 called  - module2 self

现在,module2 载入了module1,module3载入了module2,如果module3再载入一次module1会怎么样呢?

// module3.js
var 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.js
this is in module1        -  require module1
this is in module2        -  require module2
func1 from module1 called -  require module2
func1 from module1 called -  module3 self
func2 from module2 called -  module3 self

假如把module3的require顺序调整一下:

// module3.js
var in_module2 = require('./module2.js');
var in_module1 = require('./module1.js');

in_module1.func1();
in_module2.func2();

运行结果为:

rlan@rlan-LA:~/nodejs/nodetest$ node module3.js
this is in module2         - require module2
this is in module1         - require module2
func1 from module1 called  - require module2
func1 from module1 called  - module3 self
func2 from module2 called  - module3 self

看起来nodejs用某种机制保证了同一个模块在另一个模块里不会被重复载入,所以

this is in module1

这一行只出现了一次,虽然在module3.js里似乎被载入了两次。

那么,如果循环载入了会发生什么呢?现在我们让module1来require module2:

// module1.js
console.log('this is in module1');

var in_module2 = require('./module2.js');

exports.func1 = function(){
        console.log('func1 from module1 called');
}
// module2.js
console.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.js
this is in module1
this is in module2
/home/rlan/nodejs/nodetest/module2.js:4
in_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.js
this is in module2
this is in module1
func1 from module1 called

nodejs似乎阻止了载入自己的行为,运行module2的时候,行为跟module1没有载入module2的结果一样,并没有报错。而在运行module1的时候,当走到module2里面,忽略了require module1的语句之后,module2调用了module1的func1,程序出错。

综上,nodejs里嵌套重复载入模块(或者载入自己)的require语句是不能正确执行的。

“什么是nodejs模块”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


当前文章:什么是nodejs模块
链接分享:http://bjjierui.cn/article/ggieoc.html

其他资讯