Giter Site home page Giter Site logo

meteor-npm's Introduction

Complete NPM integration for Meteor Build Status

See MeteorHacks article on Complete NPM integration for Meteor

If you are migrating from Meteor 0.6.5.x to 0.6.6.x, please check this issue.


Adding NPM support to your app

Via Meteorite

mrt add npm

If you are working on multiple meteor projects at the sametime or using different versions, try to use following method instead using it with meteorite

Via NPM

npm install -g meteor-npm #single time operation
meteor-npm #type inside your project

This creates a package named npm inside your project and it has no link with meteorite. It is also included in your git. With this, you can use npm in multiple meteor projects without a problem, regardless of their versions.

Create packages.json file for listing dependencies.

{
  "redis": "0.8.2",
  "github": "0.1.8"
}

Example on using npm module inside a Meteor method

if (Meteor.isClient) {
  getGists = function getGists(user, callback) {
    Meteor.call('getGists', user, callback);
  }
}

if (Meteor.isServer) {
  Meteor.methods({
    'getGists': function getGists(user) {
      var GithubApi = Meteor.require('github');
      var github = new GithubApi({
          version: "3.0.0"
      });

      var gists = Async.runSync(function(done) {
        github.gists.getFromUser({user: 'arunoda'}, function(err, data) {
          done(null, data);
        });
      });

      return gists.result;
    }
  });
}

API

Available in the Server Side only

Meteor.require(npmModuleName)

This method loads NPM modules you've specified in the packages.json file.

var Github = Meteor.require('github');

Async Utilities

Available in the Server Side only

Meteor APIs are executed synchronously. Most of the NodeJS modules works asynchronously. So we need a way to bride the gap. Async Utilities comes to rescue you.

Async.runSync(function)

Async.runSync() pause the execution until you invoke done() callback as shown below.

var response = Async.runSync(function(done) {
  setTimeout(function() { 
    done(null, 1001);
  }, 100);
});

console.log(response.result); // 1001

done() callback takes 2 arguments. error and the result object. You can get them as the return value of the Async.runSync() as shown as response in the above example.

return value is an object and it has 2 fields. error and result.

Meteor.sync(function)

Same as Async.runSync but deprecated.

Async.wrap(function)

Wrap an asynchronous function and allow it to be run inside Meteor without callbacks.

//declare a simple async function
function delayedMessge(delay, message, callback) {
  setTimeout(function() {
    callback(null, message);
  }, delay);
}

//wrapping
var wrappedDelayedMessage = Async.wrap(delayedMessge);

//usage
Meteor.methods({
  'delayedEcho': function(message) {
    var response = wrappedDelayedMessage(500, message);
    return response;
  }
});

If the callback has a result, it will be returned from the wrapped function. If there is an error, it will be thrown.

Async.wrap(function) is very similar to Meteor._wrapAsync.

Async.wrap(object, functionName)

Very similar to Async.wrap(function), but this API can be used to wrap an instance method of an object.

var github = new GithubApi({
    version: "3.0.0"
});

//wrapping github.user.getFrom
var wrappedGetFrom = Async.wrap(github.user, 'getFrom');

Async.wrap(object, functionNameList)

Very similar to Async.wrap(object, functionName), but this API can be used to wrap multiple instance methods of an object.

var github = new GithubApi({
    version: "3.0.0"
});

//wrapping github.user.getFrom and github.user.getEmails
var wrappedGithubUser = Async.wrap(github.user, ['getFrom', 'getEmails']);

//usage
var profile = wrappedGithubUser.getFrom('arunoda');
var emails = wrappedGithubUser.getEmails();

meteor-npm's People

Contributors

apsforps avatar arunoda avatar rantav avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.