站长资讯网
最全最丰富的资讯网站

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.<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 教程》】

赞(0)
分享到: 更多 (0)