Use NodeJS Helper Script in Meteor


#1

This was originally posted on StackOverflow, but I haven’t had good luck with responses lately.

I have written a small helper module in regular NodeJS to be used with NodeJS batch scripts. I’ve placed this and all the batch scripts in the “private” folder inside my Meteor project.

I’d like to also use the helper module on the server-side of Meteor as well, but I don’t know the best way to handle that.

This is my current project structure:

client
    ... client files ...
private
    scripts
        helpers.js
        batch_script1.js
server
    ... server files ...

So for Meteor to include the “helpers.js” file into the server, it either has to be located in the “server” folder, or imported via a package. Creating a symlink won’t work, as multiple developers will be working on this and may have the repository checked out to a different directory location (seeing as how you need an absolute path to create a symlink).

I also don’t want to have to duplicate the file and maintain two copies, so what are my options for sharing a helper script between a Meteor app and a NodeJS script?

Thanks


#2
  1. Use Git Submodules
  2. Package your Nodejs file as a node pacakge and include it within one of your app’s packages like any other module.

#3

Thanks ahref,

I’m unfortunately using Subversion (as much as I would love to use Git instead, I can’t) and I don’t know if it can do submodules the same way Git can.

For option #2, would that require me to publish anything to NPM or Atmosphere? My helper already uses “module.exports” at the bottom, so how would I create the “package.js” for Meteor to import it as a node module without publishing it to NPM?


#4

You can make it a private npm or meteor package.


#5

I think I got it working. I created a “packages” directory in my Meteor project with a new directory in that for my helper, moved the helper JS file into the directory, created a “package.js” file in there as well, then used “api.addFiles” and “api.export” to create the Meteor package which I was then able to “meteor add” to my Meteor project.

As for the pure-NodeJS usage, in my script, I’m just doing something like this to require it: “var helper = require(’…/…/…/packages/helper/helper’)”. And everything seems to work now. It may not be the most elegant solution, but seems to work.

Thanks