Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

packaging node.js modules with more than code

packaging node.js modules with more than code

Problem

I'm putting together a Module I'd like to release, but am a bit stuck on how best to go about packaging it up. In addition to server side javascript, the module will need things like an admin screen, and client side javascript files. That is, it needs to serve out a fixed set of static html/css/js files. (I may have the node-static module as a dependency)

I'm curious what is the best way to handle this. I'd like to make this simple to install and integrate into apps, without forcing the user to dig through a long README. Basically they should be able to NPM the module, then add a line or two of code in the relevant place, and have it "just work". I don't want them to have to download other stuff, tell the module where to find the static files, etc.

Also, I'd like to make sure it can be included in both simple apps (i.e. one step from the standard "hello world") as well as complex apps using frameworks etc like Express, without undue hassle.

Is this possible, or is this beyond the scope of what the module system is designed to handle?

Problem courtesy of: rob

Solution

Once your package in installed with npm install mypackage -g you can use __dirname inside your executable to find the directory it's running in.

Likely /usr/local/lib/node_modules/mypackage/bin/mypackage

With your assets in /usr/local/lib/node_modules/mypackage/assets

so __dirname + '../assets' + myasset should correctly find your asset

Solution courtesy of: generalhenry

Discussion

View additional discussion.



This post first appeared on Node.js Recipes, please read the originial post: here

Share the post

packaging node.js modules with more than code

×

Subscribe to Node.js Recipes

Get updates delivered right to your inbox!

Thank you for your subscription

×