nodejs模块是什么

青灯夜游
发布: 2021-10-29 15:23:03
原创
2074人浏览过
模块是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.<anonymous> (/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.<anonymous> (/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模块是什么的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
相关标签:
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号