DucDigital
DucDigital

Reputation: 4622

Use node.js module in a Titanium app?

Currently I am writing a small Titanium app for testing. I need to include a module from NPM to titanium.

In this case I am trying with https://github.com/oortcloud/node-ddp-client

I am having the error saying Titanium couldn't find module.

The code I used for include is

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

Can I use node.js modules in Titanium?

Thank you

Upvotes: 1

Views: 1423

Answers (4)

Yahya Uddin
Yahya Uddin

Reputation: 28951

Titanium now has partial support for npm modules: http://docs.appcelerator.com/platform/latest/#!/guide/Node.js_Support

For Alloy projects, do your npm install commands in app/lib so that your packages are stored in app/lib/node_modules.

For non-alloy projects, do your npm install in Resources/ so that your packages are stored in Resources/node_modules.

Note that you may have problems with packages that rely on native node modules.

Upvotes: 2

abada henno
abada henno

Reputation: 740

You can use try this module https://github.com/smclab/titaniumifier

Get a Titanium™ SDK CommonJS module out of a Node package!

Upvotes: 2

Siwei
Siwei

Reputation: 21587

sure, why can't?

here is an example using node module in Alloy project:

1.install q.js, which will create a folder named "node module" and contain some files :

$ 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.copy the q.js to your app/lib/ folder:

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

3.declare it in your Titanium file:

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

4.use it in your controller:

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

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

Upvotes: 0

Dawson Toth
Dawson Toth

Reputation: 5680

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

It's very likely this module won't work for you. It has a lot of dependencies that use NodeJS specific modules, and specific APIs.

Luckily, someone has already written a module to connect to a Meteor server using DDP (I plead complete ignorance of this protocol and stack, by the way):

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

Upvotes: 2

Related Questions