cannot set property 'exports' of undefined

十年热恋 提交于 2019-12-13 04:49:50

问题


I have absolutely no clue why node.js makes including files from other files so difficult.

I have a file called file_handler.js

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

I would expect something like

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

to work. But I'm getting an "undefined is not a function" for upload_file(). I tried combinations of module.exports = {...} and exports = {...}. module and exports aren't even defined in my file_handler.js, so I have to set exports = {}; Which makes no sense to me since 99% of the examples on Google use module.exports as built-in.


回答1:


Okay, apparently it's because I need to load it as an AMD module.

module.exports = {...} is the CommonJS way.

define(function() {...}); is the AMD way (which I needed to use).




回答2:


It should be:

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

I have just tried this and it worked.

Alternatively, you can do something like this:

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


来源:https://stackoverflow.com/questions/33079738/cannot-set-property-exports-of-undefined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!