在 Node returns 空对象中使用 RequireJS 导出函数
Exporting a function with RequireJS in Node returns an empty object
如何从 Node 中的 RequireJS 模块导出函数?使用我的代码,我得到一个空对象,而不是我期望的 Backbone 模型。
first.js
包含:
'use strict';
var define=require('amd-define');
define(function (require) {
var Backbone = require('backbone');
// Our basic **Todo** model has `title`, `order`, and `completed` attributes.
var Todo = Backbone.Model.extend({
// Customizations of my model...
});
return Todo;
})
我的测试文件 test.js
包含:
'use strict';
var chai =require("chai");
var assert=chai.assert;
var expect=chai.expect;
var Todo=require("first");
describe('Tests for Todo model', function () {
it('should create global variables for Todo', function () {
expect(Todo).to.be.exist;
console.log(typeof (Todo))
});
it('should be created with default values for its attributes', function() {
var todo = new Todo();
expect(todo.get('title')).to.equal('');
});
it('should fire a custom event when state change', function() {
var todo = new Todo();
todo.set({completed: true, order: 1});
todo.set('title', 'my title');
});
});
它给出了 Todo
不是函数的错误。 console.log
语句打印 object
.
软件包 amd-define
有问题或不支持 CommonJS sugar。 (后者可能是这种情况。我没有看到其中的任何代码负责进行支持 CommonJS 糖所需的依赖转换。)
我建议放弃 amd-define
并改用 amd-loader
。我已经用了很多年了,它很管用。
对于您的代码:
从 first.js
中删除 var define=require('amd-define');
。
在你的测试文件中添加 require('amd-loader')
(安装后)在你加载任何 AMD 模块之前。
我能够从 first.js
中导出。
如何从 Node 中的 RequireJS 模块导出函数?使用我的代码,我得到一个空对象,而不是我期望的 Backbone 模型。
first.js
包含:
'use strict';
var define=require('amd-define');
define(function (require) {
var Backbone = require('backbone');
// Our basic **Todo** model has `title`, `order`, and `completed` attributes.
var Todo = Backbone.Model.extend({
// Customizations of my model...
});
return Todo;
})
我的测试文件 test.js
包含:
'use strict';
var chai =require("chai");
var assert=chai.assert;
var expect=chai.expect;
var Todo=require("first");
describe('Tests for Todo model', function () {
it('should create global variables for Todo', function () {
expect(Todo).to.be.exist;
console.log(typeof (Todo))
});
it('should be created with default values for its attributes', function() {
var todo = new Todo();
expect(todo.get('title')).to.equal('');
});
it('should fire a custom event when state change', function() {
var todo = new Todo();
todo.set({completed: true, order: 1});
todo.set('title', 'my title');
});
});
它给出了 Todo
不是函数的错误。 console.log
语句打印 object
.
软件包 amd-define
有问题或不支持 CommonJS sugar。 (后者可能是这种情况。我没有看到其中的任何代码负责进行支持 CommonJS 糖所需的依赖转换。)
我建议放弃 amd-define
并改用 amd-loader
。我已经用了很多年了,它很管用。
对于您的代码:
从
first.js
中删除var define=require('amd-define');
。在你的测试文件中添加
require('amd-loader')
(安装后)在你加载任何 AMD 模块之前。
我能够从 first.js
中导出。