Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Including a .js file in node.js

Tags:

node.js

I am trying to include this script in my app: http://www.movable-type.co.uk/scripts/latlong.html

I have saved it in a file called lib/latlon.js, and I am trying to include it like this:

require('./lib/latlon.js');

How should I go about including a JS library like this?

like image 938
Tom Avatar asked Sep 05 '11 15:09

Tom


1 Answers

First of all, you should take a look at the Modules documentation for node.js: http://nodejs.org/docs/v0.5.5/api/modules.html

The script you're trying to include is not a node.js module, so you should make a few changes to it. As there is no shared global scope between the modules in node.js you need to add all the methods you want to access to the exports object. If you add this line to your latlon.js file:

exports.LatLon = LatLon;

...you should be able to access the LatLon function like this:

var LatLonModule = require('./lib/latlon.js');
var latlongObj = new LatLonModule.LatLon(lat, lon, rad);
like image 160
Jørgen Avatar answered Oct 05 '22 06:10

Jørgen