How to use npm module in Meteor client?

I am completely confused about how to use the npm module in Meteor client code.

I understand that modules like fs will only work on the server side, but in this case I would like to use a simple text module to display beautiful dates:

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

I tried installing the module under / public / node_modules, and it works fine on the server side, following these instructions from SO: ( How can we or can we use node modules through npm using Meteor? )

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

However, it does not work in client 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" 

In this case, the server side is useless to me as I try to display text on the client.

+7
source share
1 answer

I do not think you need to use the server version. Use npm stuff only on server side and btw, put it in your / public /. Who knows, maybe you can call him when he is in your / public /, try it. Or try this one.

Use something like jquery timeago.js

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

Create / client / helpers.js or some of them.

Use the helper helm.

 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'; }); 

An example of calling the date function handlebars helper from a template.

 {{ date created }} 

Where the date is handebars helper and a date is created that leaves the meteor / mongo collection.

See the github Britto project. This is where I got this piece of code and used it in a chat application that I wrote. It works great.

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

+6
source

All Articles