无法设置未定义的 属性 'exports'

cannot set property 'exports' of undefined

我完全不知道为什么 node.js 使得从其他文件中包含文件如此困难。

我有一个名为 file_handler.js

的文件
exports = {};
exports = {
    upload_file: function (fileUploaderPath, filename) {
        var child_process = require('intern/dojo/node!child_process');
        child_process.spawn(fileUploaderPath + ' ' + filename);
    }
};

我希望是这样的

var file_handler = require('./file_handler.js');
file_handler.upload_file(a,b);

上班。但是我得到了 "undefined is not a function" for upload_file()。我尝试了 module.exports = {...} 和 exports = {...} 的组合。模块和导出甚至没有在我的 file_handler.js 中定义,所以我必须设置 exports = {};这对我来说毫无意义,因为 Google 上 99% 的示例都使用 module.exports 作为内置。

应该是:

module.exports = {
    upload_file: function (fileUploaderPath, filename) {
        var child_process = require('intern/dojo/node!child_process');
        child_process.spawn(fileUploaderPath + ' ' + filename);
    }
};

我刚试过这个并且成功了。

或者,您可以这样做:

exports.upload_file=function (fileUploaderPath, filename) {
  var child_process = require('intern/dojo/node!child_process');
  child_process.spawn(fileUploaderPath + ' ' + filename);
};

好吧,显然是因为我需要将它作为 AMD 模块加载。

module.exports = {...} 是 CommonJS 方式。

定义(函数(){...});是 AMD 方式(我需要使用)。