在Titanium应用程序中使用node.js模块?

时间:2014-05-21 16:52:32

标签: javascript node.js titanium

目前我正在编写一个小型Titanium应用程序进行测试。我需要在NPM中加入一个钛模块。

在这种情况下,我正在尝试使用https://github.com/oortcloud/node-ddp-client

我有错误称Titanium无法找到模块。

我用于包含的代码是

var DDPClient = require("./lib/node_modules/ddp");

我可以在Titanium中使用node.js模块吗?

谢谢

4 个答案:

答案 0 :(得分:2)

require('./lib/node_modules/ddp/lib/ddp-client.js');

这个模块很可能不适合你。它有很多使用NodeJS特定模块和特定API的依赖项。

幸运的是,有人已经编写了一个使用DDP连接到Meteor服务器的模块(顺便说一句,我完全不了解这个协议和堆栈):

https://github.com/yubozhao/Ti.Meteor

答案 1 :(得分:2)

您可以尝试使用此模块https://github.com/smclab/titaniumifier

  

从Node包中获取Titanium™SDK CommonJS模块!

答案 2 :(得分:2)

Titanium现在部分支持npm模块:http://docs.appcelerator.com/platform/latest/#!/guide/Node.js_Support

对于Alloy项目,请在npm install中执行app/lib命令,以便将您的包存储在app/lib/node_modules中。

对于非合金项目,请在npm中安装Resources/,以便将您的包存储在Resources/node_modules

请注意,您可能遇到依赖本机节点模块的软件包的问题。

答案 3 :(得分:0)

确定,为什么不能?

这是使用Alloy项目中的节点模块的示例:

1.install q.js,它将创建一个名为“node module”的文件夹并包含一些文件:

$ npm install q
$ find node_module
node_modules/
node_modules/q
node_modules/q/README.md
node_modules/q/queue.js
node_modules/q/package.json
node_modules/q/q.js
node_modules/q/LICENSE

2.将q.js复制到你的app / lib /文件夹:

$ mkdir app/lib
$ cp node_modules/q/q.js app/lib

3.在你的Titanium文件中声明它:

// in app/alloy.js
Q = require('q')

4.在你的控制器中使用它:

// app/controllers/index.js:
var defer = Q.defer();

请参阅:http://developer.appcelerator.com/question/154529/how-to-use-nodejs-modules-with-titanium#answer-285207