Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference local files in a npm module?

Tags:

node.js

npm

I wrote a simple npm module to precompile my handlebars templates when using django compressor to do post-processing for some client side components and found that I need to ship the npm module with a few js files.

Currently I just assume no one is installing this with the global flag because I've "hard coded" the path to these dependencies in the npm module itself

example layout of my npm module

/
* /bin
* /lib/main.js
* /vendor/ember.js

Now inside main.js I want to use the ember.js file ... currently my hard coded approach looks like this

var emberjs = fs.readFileSync('node_modules/django-ember-precompile/vendor/ember.js', 'utf8');

Again -this only works because I assume you install it local but I'd like to think node.js has a more legit way to get locally embedded files

Anyone know how I can improve this to be more "global" friendly?

like image 590
Toran Billups Avatar asked Jan 19 '13 19:01

Toran Billups


People also ask

How use npm local link?

Example: Let the local-dir is the local directory and project-dir is the project directory and local_module is the local module package you want to install, first go to the local-dir and type npm link and next go to the project directory and type npm link <local_module> this will link your local module to your project.

How do I view local files in node JS?

To do this, you'll need to import the fs module, a standard Node. js module for working with files, and then use the module's readFile() function. Your program will read the file, store its contents in a variable, then log its contents to the console.


2 Answers

What you can do is get the directory of the current file and make your file paths relative to that.

var path = require('path')
, fs = require('fs');

var vendor = path.join(path.dirname(fs.realpathSync(__filename)), '../vendor');
var emberjs = fs.readFileSync(vendor + '/ember.js', 'utf8');

Hope that helps!

like image 98
Matthew J Morrison Avatar answered Oct 11 '22 18:10

Matthew J Morrison


One of the great strengths of Node.js is how quickly you can get up and running. The downside to this approach is that you are forced to fit the design patterns it was build around.

This is an example where your approach differs too much from Nodes approach. Node expects everything in a module to be exposed from the modules exports, including templates.

Move the readFileSync into the django-ember-precompile module, then expose the returned value via a module export in lib/main.js.

Example:

package.json
    { 
    "name": "django-ember-precompile",
    "main": "lib/main.js"
    }
lib/main.js
    module.exports.ember = readFileSync('vendor/ember.js')
vendor/ember.js

You obtain your template via

var template = require('django-ember-precompile').ember

This example can be refined, but the core idea is the same.

like image 26
Jacob Groundwater Avatar answered Oct 11 '22 19:10

Jacob Groundwater