11

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?

2
  • nope, according to your link: "only Mozilla Firefox with JavaScript version 1.7 supports process-based programming model via the yield keyword" Commented Jan 16, 2014 at 19:50
  • 1
    "The SIM.JS library provides event-based programming model, for the following reasons:...only Mozilla...supports process-based programming". So in my reading, they are not using yield and doing idiomatic javascript. Reviewing the source there is no use of yield I can see simjs.com/_downloads/sim-0.26-debug.js Commented Jan 16, 2014 at 20:10

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Its comforting to hear validation its a mess, like a mosh pit of libraries from what I can tell. ;) Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.