Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use npm module in Meteor client?

Tags:

node.js

meteor

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.

like image 628
7zark7 Avatar asked May 08 '12 21:05

7zark7


People also ask

Can I use npm module in browser?

If you simply want to test out some NPM modules right inside your browser without setting up an entire app, you can use Browserify in three simple steps to use NPM modules. Looking to improve your skills? Check out our interactive course to master JavaScript from start to finish.

How do I run a node module?

The usual way to run a Node. js program is to run the globally available node command (once you install Node. js) and pass the name of the file you want to execute. While running the command, make sure you are in the same directory which contains the app.

How do I run npm in Visual Studio?

To open the package manager, from Solution Explorer, right-click the npm node in your project. Next, you can search for npm packages, select one, and install by selecting Install Package.


1 Answers

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.

like image 102
Steeve Cannon Avatar answered Oct 05 '22 04:10

Steeve Cannon