7

I'm thoroughly confused on how to use an npm module in Meteor client code.

I understand modules like fs would only work server-side, but in this case I'd like to use a simple text module like this for displaying pretty dates:

https://github.com/ecto/node-timeago

I've tried installing the module under /public/node_modules, and it works great on the server-side following these instructions from SO: ( How do we or can we use node modules via npm with Meteor?)

Meteor.startup(function () {
  var require = __meteor_bootstrap__.require
  var timeago = require('timeago')
  console.log(timeago(new Date()))
  ...

However it doesn't work in the client-side code:

if (Meteor.is_client) {
  var require = __meteor_bootstrap__.require
  var timeago = require('timeago')
  console.log(timeago(new Date()))
  ...

Uncaught ReferenceError: __meteor_bootstrap__ is not defined"

Server-side is sort of useless for me in this case, as I'm trying to render text on the client.

Community
  • 1
  • 1
7zark7
  • 10,015
  • 5
  • 39
  • 54

1 Answers1

6

I don't believe you need to use the server side version. Use the npm stuff for server side only and btw, put it in your /public/ as well. Who knows maybe you can call it once it is in your /public/, try it. Or try this.

Use something like the jquery timeago.js

Put it in /client/ or something like /client/js

Create a /client/helpers.js or some such.

Use a handlebars helper.

Handlebars.registerHelper('date', function(date) {
  if(date) {
    dateObj = new Date(date);
    return $.timeago(dateObj);
  }
  return 'a long long time ago in a galaxy far away';
});

Example of calling 'date' handlebars helper function from template.

{{ date created }}

Where date is the handebars helper and created is the date coming out of the meteor/mongo collection.

See the github Britto project. That is where I got this code snippet and used it in a chat room app I wrote. Works fine.

There are a couple of others floating out there. Go to madewith.meteor.com and peruse the source of some of the projects.

Steeve Cannon
  • 3,682
  • 3
  • 36
  • 49
  • Thanks mate! So a summary of issue is: * npm modules aren't optimal for client side * Handlebar helpers are your friend * jQuery plugins can be used and referenced automatically if they are in the app dir – 7zark7 May 08 '12 at 22:31
  • Correction "app /client dir" - as you will see "ReferenceError: jQuery is not defined" otherwise. – 7zark7 May 08 '12 at 22:45
  • yeah, it is working for me, I used the Britto example from JonathanKingston's code...there is another project that has a good human readable date too...search through the maddewith.meteor.com projects – Steeve Cannon May 08 '12 at 23:39
  • 1
    @7zark7 You should try the an npm install in your /public/ directory though if you want to use node modules. I wasn't sure if I was clear about that part. – Steeve Cannon May 09 '12 at 11:33
  • 1
    @SteeveCannon, I suggest a package.json with your node module dependencies specified, and a scripts.install to move the node_modules folder into public/ after those deps are installed. I've written an example project, which will also deploy to Heroku: https://github.com/matb33/heroku-meteor-npm. – matb33 May 29 '12 at 15:54