Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting an external javascript library to a Node.js module

I ran across sim.js, a library for discrete event simulation, and I thought it would be fun to use while learning node.js. It's a single javascript file, with no dependencies on other libraries or the browser.

What's the best practice for including this in my code base? Save the .js to disk, and convert it to a standard node module?

I.e.:

// ---sim.js
var Sim = function () {
  this.simTime = 0;
  this.entities = [];
}

Sim.Event = function (name) {
  this.name = name;
};
// ... functions continue
module.exports = Sim;

// --- main.js
var Sim = require('./sim');

var sim = new Sim();
var trafficLights = [new Sim.Event("North-South Light"),
                     new Sim.Event("East-West Light")]; 

Or is there an easier way that would not require modifying/modularizing the original js?

like image 443
Allyl Isocyanate Avatar asked Jan 16 '14 19:01

Allyl Isocyanate


People also ask

How do I import an external library into node js?

Command “require” is used in Node JS for import external libraries. Mentioned below is an example of this. “var http=require (“http”)” . This will load the library and the single exported object through the HTTP variable.

How modules can be available externally in node JS?

js Package Manager (npm) is the default and most popular package manager in Node. js ecosystem that is primarily used to install and maintain external modules in Node. js application. Users can basically install the node modules needed for their application using npm.

How do you call one js file from another js file in node JS?

To include functions defined in another file in Node. js, we need to import the module. we will use the require keyword at the top of the file. The result of require is then stored in a variable which is used to invoke the functions using the dot notation.


1 Answers

So yes, if you just want to make this work, you can save it to disk. It looks like Sim is designed for browser-global approach, which means it doesn't know anything about the CommonJS patterns that a node module must support. So you could add those like your like above module.exports = Sim; snippet above, and that would basically get you up and running.

If you wanted to get this project to support node/CommonJS you could send them a patch. Many libraries that began life in the browser have had to add support for package managers such as requirejs, commonjs, amd, bower, component, etc. For example, the moment library supports a lot of different types of uses, and you can see lots of bits of config and glue code necessary to make it work so broadly.

I wouldn't worry too much about "best practices" as this whole space is a freaking mess right now, but I'd point to moment.js as a good example of a project that works well in many different environments and packaging systems.

like image 109
Peter Lyons Avatar answered Oct 22 '22 22:10

Peter Lyons