最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - cannot set property 'exports' of undefined - Stack Overflow

matteradmin6PV0评论

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 binations 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.

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 binations 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.

Share Improve this question edited Oct 12, 2015 at 11:18 Tunaki 138k46 gold badges366 silver badges438 bronze badges asked Oct 12, 2015 at 11:15 BranBran 6478 silver badges22 bronze badges 1
  • Would it be possible to show the entire file_handler.js file, or at least how it begins and ends? – Ken Franqueiro Commented Oct 13, 2015 at 0:19
Add a ment  | 

2 Answers 2

Reset to default 3

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).

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);
};
Post a comment

comment list (0)

  1. No comments so far