定制现有的模块

我有一个帮助函数的集合,我喜欢将它们与现有的实用程序模块合并在一起。

不知何故如此:

var customUtil = require('customUtilites'); customUtil.anotherCustomFunction = function() { ... }; exports = customUtil; 

这可以以某种方式实现吗?

你完全可以这样做。

例如

customUtilities.js:

 module.exports = { name: 'Custom' }; 

helperA.js

 module.exports = function() { console.log('A'); } 

helperB.js:

 module.exports = function() { console.log('B'); } 

bundledUtilities.js:

 var customUtilities = require('./customUtilities'); customUtilities.helperA = require('./helperA'); customUtilities.helperB = require('./helperB'); module.exports = customUtilities; 

main.js:

 var utilities = require('./bundledUtilities'); utilities.helperA(); 

运行node main.js你会看到A打印的。