node绝对和相对模块

绝对模块是值Node通过在其内部node_modules查找到的模块,或者Node内置的如fs这样的模块。

相对模块将require指向一个相对工作目录中的JavaScript文件。

比如,我们在同一目录中创建fileOne.js、 fileTwo.js、  index.js这三个文件

fileOne.js中内容为

console.log('this is fileOne');

fileTwo.js中内容为

console.log('this is fileTwo');

index.js中内容为

require('fileOne');
require('fileTwo');

运行index.js文件  

结果未能找到fileOne和fileTwo,因为它们并不是通过NPM来安装,也不在node_modules目录中,且Node自带模块中没有以此为名的模块。

如果想让它正确显示出来,我们只需要在require前面加上./

require('./fileOne');
require('./fileTwo');

  

  

猜你喜欢

转载自www.cnblogs.com/ddkei/p/8921473.html
今日推荐