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
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
You can use try this module https://github.com/smclab/titaniumifier
Get a Titaniumâ„¢ SDK CommonJS module out of a Node package!
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.
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
Related
this is probably a silly question but am new to Meteor and struggling a bit. I want to build a stellar app that tweets when you get stellar. There is a nice Javascript API stellar-lib that works on node, but im unsure how to access the modules in Meteor...
I'm also building an app with Meteor and stellar-lib and have found two options:
1) You can manually copy over the built stellar-lib.js or stellar-lib-min.js from the build/ directory from either the github repo or the node_modules folder you installed it to when you ran the npm install command.
If you do this, you will have to copy the .js file to client/compatibility, otherwise stellar-lib will not work (note: this means you can only use Stellar on the client).
2) If you need it on the server, you can also have browserify wrap stellar-lib for you, then copy the output to lib/ in your Meteor app's root directory. I did this in my repo here with gulp.
Here's a short explanation to how 2) works:
.gulp is where I'll install my NPM modules where they will be ignored by the Meteor build environment (because the folder is hidden).
In deps.js, I require the modules I would want to use in my Meteor app as I would if I was using them in a traditional node.js app. package.json defines the modules I'll install with NPM and the gulpfile.js describes a build task that will resolve my require statements and output a single deps.js file that includes my dependencies to my Meteor app's lib/ folder.
What is the best way of sharing code between frontend and backend using javascript, specifically saying between nodejs and angularjs?
Thing is that we are using same enums and constant values such as error codes in both backend and frontend. Right now we just copy&paste each change to both platform which isn't a good solution. There are also some services that could be shared.
I have seen libraries such as browserify; but thats not exactly what I am looking for. I am looking for a solution similar to maven dependency in java. In java, libraries can be shared easily using maven, whereas I can't find a similar way of doing that in javascript.
Is there a way to isolate these services and give them as dependency to nodejs using npm and to angularjs using bower independently? Or what are the ways for sharing the same code between frontend and backend?
There are several ways to do this. The first is that you can make a new package which is required via bower for the front-end code and via npm for the backend code. I have several packages published to both systems.
Install with Bower -- information on how to install modules that aren't in the registry
NPM Install docs -- all the ways to install with npm (private github with auth: git+ssh://git#github.com/[org]/[repo])
Just create a new module with your shared data and install it using both package managers. Both of them allow you to install an unpublished module so if it's private data you can keep it as such.
If your front end requires require.js you can use something like amdefine to make it available to your node backend, or if you're just using legacy window code you can do something like:
var mydata = {};
if(typeof window !== 'undefined'){
window.mydata = mydata;
} else {
module.exports = mydata;
}
If you are sharing a lot of data though I highly recommend looking into browserify to write your entire codebase in commonjs and using browserify to generate your client bundle. There is a laundry list of resources about using browserify, including stuff on how to use browserify and angular together
Disclaimer - I'm still developing this approach and it's a little manual.
I did this using npm, an npm cli called pac, and bower. Pac let's me avoid using npm install in production by keeping the modules as .tgz files (committed to project in source control). With pac, when someone checks out the node project, they run pac install then npm rebuild instead of npm install.
My shared code is kept in a directory (my-module). It has both package.json and a bower.json.
My consuming node app has a package.json dependency for:
"my-module" : "x.y.z"
My consuming client has a bower.json dependency for:
"my-module" : "../relative/path/to/my-module"
When I make updates to my-module, I update my node app by:
Making a tar.gz of the contents of my-module:
tar -czvf my-module.tar.gz -C my-module
Removing the old version from the node app's node_modules
Rerunning npm install path/to/my-module-tar.gz
Rerunning pac (this makes a .tgz of node_modules/my-module)
Committing the updated pac .modules/my-module.tgz
I update my client by:
Removing the old client/bower_components/my-module
Rerunning bower install or bower update
I have been trying to use the Npm.require to get the ldapjs module and use it for client authentication, however I am getting the following message.
var ldap = Npm.require('ldapjs');
Error: Cannot find module 'ldapjs'
Isn't the require supposed to download the package from the npm?
Currently the best way to use NPM packages in Meteor is this npm smart package. The instructions on how to use it are pretty clear there. Basically, you do three things:
1) Install npm:
mrt add npm
2) Create packages.json file with list of required packages:
{
"ldapjs": "0.6.3"
}
3) Use the package via Meteor.require:
var ldapjs = Meteor.require('ldapjs');
Nope, it is not. Meteor will only download a node module as long as it is declared within a smart package, with Npm.depends({...}) directive. If your code is not a part of some smart package, then you'll need to install the node module manually.
You need two things to use npm modules in Meteor packages:
Npm.depends - specify the modules you want to use, with versions. Meteor's build system will download the package and manage its dependencies
Npm.require - pull in a module, making it available in the current scope
Note that you need to write a package to use an npm module. You'll probably want to read through the Meteor docs on packages.
For an example, check out the logging package in Meteor. Its package.js specifies a depency on the npm module cli-color, and its logging.js file requires and uses the module.
node modules in the main application
What if you want to use npm from the main application? And what if you don't want to install node modules manually (a maintenance headache)?
This is possible with a workaround. Create a shim smart package to provide the node modules for the main application. Export the modules to the main application.
The exact steps
1. Create a directory npm-shim outside your Meteor application. We will use it in step 3.
2. Add these two files to it:
File package.js
// npm dependencies are only available for packages. If you have npm
// dependencies for the main application, you need this workaround:
// Create a shim package to provide the npm dependencies to the main app.
Package.describe({
summary: "Meteor main application npm dependencies"
});
Npm.depends({
colors: '0.6.2',
// Add more modules as needed.
});
Package.on_use(function(api) {
api.export("NpmShim"); // Omit this for versions before 0.6.5
api.add_files("npm.js", "server");
});
File npm.js
NpmShim = {};
NpmShim.colors = Npm.require('colors');
// Add more modules as needed.
3. Edit smart.json and add this line:
"npm-shim": { "path": <path to the directory created in step 1> },
4. Execute mrt update then meteor add npm-shim.
The result of this workaround
Node modules can be used from the Meteor main application without the need to manually install them. Use NpmShim.colors instead of Npm.require('colors').
If you need more modules, you have to add them to package.js and npm.js (see comment // Add more modules as needed).
I'm using :
Mojito 0.5.5
NPM 1.2.14
NodeJS 0.8.22
I'm trying to create a simple Mojito application using NodeJS modules, packaged with NPM.
Documentation say that :
Create a Mojito application.
Add any needed dependencies to dependencies object in package.json.
Install dependencies with npm: {app_dir}$ npm install
When Mojito starts, your application will have access to the installed npm modules.
Indeed, the example above isn't working.
"dependencies": {
"mojito": "0.5.x",
"async": "0.2.x"
},
status: function(ac) {
var async = require('async');
ac.done({
status: 'loaded'
});
}
Mojito server : "Error: Cannot find module 'async'"
Web page error : "Error: MODULE_NOT_FOUND"
Any clue ?
PS : Of course, after npm install, the project folder have a node_modules folder with the async package.
As today, mojito npm package is used as a cli but it also holds the mojito core. This is problematic because most developers will run the app by doing mojito start, which runs from the global mojito package. This, plus the fact that the current YUI is messing with the require, it causes a lot of troubles when requiring dependencies.
The solution is simple, make sure you install mojito locally in your application, and run the app by doing ./node_modules/mojito/bin/mojito start or by doing node server.js instead of mojito start.
Aside from that, we are working on splitting the cli and core into separate packages, and waiting for YUI 3.9.0 to solve this problem once for all so developers can keep using mojito start.
How do we or can we use node modules via npm with Meteor?
Or is that something that will be dependent on the packaging API?
Or is there a prescribed method that is recommended?
Meteor 1.3, released on March 28, 2016, gives apps full ES6 (ES2015) modules support and out of the box NPM support. Apps and packages can now load NPM modules directly and easily on the client and on the server.
If you can use 1.3, then check http://guide.meteor.com/using-packages.html#installing-npm.
For example, to use moment.js:
meteor npm install --save moment
Then in your code:
import moment from 'moment';
// this is equivalent to the standard node require:
const moment = require('moment');
If you need to use an older version of Meteor, read the rest of the answer below.
Pre-Meteor 1.3:
Since v0.6.0, Meteor integrates directly with NPM modules with the help of a 3rd party package. For example, to use a module like ws,
Run sudo npm install -g ws (or for local installs, see this)
In your sever JavaScript file,
var Websocket = Npm.require('ws');
var myws = new Websocket('url');
To use a core Node module, just make the corresponding Npm.require() call, e.g. var Readable = Npm.require('stream').Readable.
You can use any of the more than 230,000 NPM modules directly with Meteor thanks to the NPM package developed by Arunoda.
You can also define dependencies on Npm packages from smart packages - from the initial announcement of npm support:
Your smart package can now define dependencies directly, by adding a call to Npm.depends in package.js:
Npm.depends({
"awssum": "0.12.2",
"underscore.string": "2.3.1"
});
All of this works well with hot code reload, just like the rest of Meteor. When you make changes, the bundler will automatically download missing npm packages and re-pin its dependencies.
To use an NPM module within server code, use Npm.require as you would normally use plain require. Notably, __meteor_bootstrap__.require has been eliminated and all of its uses have been converted to Npm.require.
There is a small example of using an NPM module in your application.
Note that this answer applies to versions of Meteor prior to 0.6.0, which was released in April 2013 and added direct npm integration
Install modules as you normally would through npm and then use
var require = __meteor_bootstrap__.require,
pd = require("pd"),
after = require("after") // etc
Load any modules you want
I did a complete write-up on this on Meteorpedia:
http://www.meteorpedia.com/read/npm
The article covers how to use npm in both your app and/or packages, and common patterns for wrapping regular callbacks and event emmitter callbacks to work properly in Meteor and Fibers, and include's references to Arunoda's async-utilities and additional resources.
You could use the Meteor Npm package
meteor add meteorhacks:npm
Then create a packages.json file in your project's root directory with the NPM module's info.
{
"redis": "0.8.2",
"github": "0.1.8"
}
Then as simple as (server side)
var github = Meteor.npmRequire("github");
var redis = Meteor.npmRequire("redis");
So you just use Meteor.npmRequire instead of require
I wrote a Gist on how to do this as of Meteor 0.6.5, How to add Node.js npms to your Meteor.js project.
I am using such a script which nicely install all Node.js dependencies. It behaves similar to the official support in the Meteor engine branch (it installs dependencies at runtime), but it also supports installing from Git repositories and similar goodies.