【问题标题】:cannot set property 'exports' of undefined无法设置未定义的属性“出口”
【发布时间】:2016-01-09 20:24:32
【问题描述】:

我完全不知道为什么 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);

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

【问题讨论】:

  • 是否可以显示整个file_handler.js 文件,或者至少显示它的开始和结束方式?

标签: javascript node.js intern


【解决方案1】:

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

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

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

【讨论】:

  • 你能提供一个完整的例子来说明define 用于upload_file吗?
【解决方案2】:

应该是:

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);
};

【讨论】:

  • 是的,我试过了,但是当我运行我的代码时,它说模块未定义(对于第一个示例),而导出对于第二个示例未定义。
猜你喜欢
  • 2021-11-27
  • 2022-10-30
  • 2023-02-10
  • 2015-12-07
  • 2013-05-28
  • 2016-02-23
  • 2011-11-20
相关资源
最近更新 更多