• Stars
    star
    508
  • Rank 86,941 (Top 2 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 11 years ago
  • Updated about 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Complete NPM integration for Meteor

Use Npm Modules with Your Meteor App

Using With Meteor 1.3

Meteor 1.3 has the build in NPM support. So, in Meteor 1.3 this package won't add anything.

Migration Guide

If your app use this package, follow these steps to migrate into 1.3.

  • Create a package.json in your app. Use: npm init.
  • Move all the packages on packages.json into package.json dependencies.
  • Then do a npm install to install those NPM module.
  • Remove meteorhacks:npm from your app with: meteor remove meteorhacks:npm.
  • Remove npm-container from your app with: meteor remove npm-container.
  • If you use Async methods introduced by this package, use meteorhacks:async package directly.

With Meteor you only can use npm modules inside packages. You can't directly use npm modules with meteor apps. This package solves that issue :)

Installation

meteor add meteorhacks:npm

Then start your app with meteor and follow the instructions.

Defining Packages

Once npm support has been initialized, you'll have a file name called packages.json inside your app. Define packages in that file as shown below.

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

You must define an absolute version number for the npm module

If you need to install an npm module from a specific commit, use the syntax:

{
  "googleapis": "https://github.com/bradvogel/google-api-nodejs-client/archive/d945dabf416d58177b0c14da64e0d6038f0cc47b.tar.gz"
}

The above can be generated using github releases. You're going to want to use <commit hash>.tar.gz version, not archive/<version number>.tar.gz.

Using Packages

You can use Meteor.npmRequire method to access the npm module on server side and use it as you want. Most of the npm modules provide asynchronous API's with callbacks or promises. So, you can't directly use them with Meteor. Because of that, this package comes with a handy set of Async utilities make your life easier.

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.npmRequire('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.npmRequire(npmModuleName)

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

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

Meteor.require(npmModuleName)

Same as above. But deprecated.

Async Utilities

Available in the Server Side only Async Utitlies is available as a separate package via meteorhacks:async

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 delayedMessage(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();

More Repositories

1

cluster

Clustering solution for Meteor with load balancing and service discovery
JavaScript
631
star
2

meteord

MeteorD - Docker Runtime for Meteor Apps for Production Deployments
Shell
439
star
3

meteor-ssr

Server Side Rendering for Meteor
JavaScript
264
star
4

sikka

Sikka - A Firewall for Meteor Apps
JavaScript
258
star
5

kadira

Performance Monitoring for Meteor
JavaScript
217
star
6

meteor-aggregate

Proper MongoDB aggregations support for Meteor
JavaScript
189
star
7

picker

Server Side Router for Meteor
JavaScript
182
star
8

meteor-down

Load testing for Meteor
JavaScript
175
star
9

search-source

Reactive Data Source for Search
JavaScript
146
star
10

flow-components

Build your Meteor app with Components.
JavaScript
132
star
11

unblock

this.unblock inside publications :D
JavaScript
87
star
12

meteor-inject-initial

Allow injection of arbitrary data to initial Meteor HTML page
JavaScript
80
star
13

meteor-async

Set of async utilities to work with NPM modules inside Meteor
JavaScript
63
star
14

goddp

DDP Server implemented with golang
Go
47
star
15

meteorx

Exposing some of the internal Meteor API prototypes
JavaScript
41
star
16

mup-frontend-server

Frontend Server for Meteor Up
Nginx
37
star
17

kube-init

Easiest way to deploy a Kubernetes Cluster to learn Kubernetes
Shell
34
star
18

zones

Zone.js integration for meteor
JavaScript
30
star
19

meteorhacks.github.io

MeteorHacks Website
HTML
29
star
20

hyperkube

Hyperkube
Shell
20
star
21

inject-data

A way to inject data to the client with initial HTML
JavaScript
18
star
22

kadira-profiler

CPU Profiling support for Kadira
JavaScript
14
star
23

bddp

DDP like binary protocol implemented using cap'n proto
Go
12
star
24

repeeet

making repeeet tweeting super simple
JavaScript
11
star
25

code-standards

Code Standards for MeteorHacks projects
9
star
26

js-pipes

MongoDB aggregation pipeline implementation in JavaScript
JavaScript
9
star
27

meteor-customer-io

Customer.io Integration for Meteor
JavaScript
9
star
28

cluster-performance

Performance Test for Cluster
JavaScript
8
star
29

docker-librato

Forward all stats from all running docker containers to Librato
JavaScript
8
star
30

kdb

ACID High Performance Time Series DB for any kind of storage - No it isn't
Go
7
star
31

meteor-todo-app

Simple Todo App with Meteor
JavaScript
5
star
32

gocluster

Meteor Cluster protocol implemented for Go
Go
4
star
33

meteor-collection-utils

Expose some underline collection apis
JavaScript
4
star
34

find-faster-chat-demo

Simple Chat To Demo Meteor Fast Finder Use
JavaScript
3
star
35

ddptest

Test DDP servers
JavaScript
3
star
36

zones-simple-example

Simple Example Meteor App with Zones
JavaScript
2
star
37

mongo-search

Simple API to use MongoDB Full Text search with Meteor
2
star
38

kmdb

metric database powered by kdb
Go
2
star
39

meteor-down-backdoor

Backdoor Meteor package for MeteorDown
JavaScript
2
star
40

kadira-binary-deps

Binary Dependencies for Kadira
JavaScript
2
star
41

simple-rpc-go

A really simple and fast binary RPC middleware
Go
2
star
42

kmdb-node

NodeJS client for kmdb
JavaScript
2
star
43

nsqd-to-librato

Send NSQ stats to librato
JavaScript
2
star
44

lean-components-example

lean-components-example
JavaScript
2
star
45

meteor-zone-example

Example meteor app for meteor-zone package
JavaScript
1
star
46

node-histo-utils

A set of utilities to create, merge and manage histograms
JavaScript
1
star
47

blaze-ext

Create blaze templates with .blaze extension
1
star