packages

node-packages is require.js for node.js.

node-packages is require.js for node.js.

  • Flattens & simplifies application structure.
  • Better encapsulation.
  • More consistent.
- package.json - application package
- lib/ - javascript source files
  - index.js - main entry point
    - packages/ - application components
      - config/ - application config component
      - http.server/ - http server component
        - index.js   - package entry point
      - http.routes/ - http routes
        - index.js   

In lib/index.js, you'll need something like:

var packages = require("packages");
 
packages.
require(__dirname + "/packages").
load();

In http.server/index.js, you might have something like:

exports.require = ["express", "config"];
exports.load = function (expressconfig) {
  var app = express()
  app.listen(config.get("port"));
  return app;
};

In http.routes/index.js, you might have something like:

exports.require = ["http.server"];
exports.load = function (app) {
  app.get("/hello", function (reqres) {
    res.send("Hello World!");
  });
};

Initializes new packages loader

Adds a package. Below's an example where this might be useful.

lib/index.js:

var packages = require("packages");
 
module.exports = function (config) {
  packages.
  add("config", config).
  require(__dirname + "/packages").
  load();
}

lib/packages/http.server/index.js:

exports.require = ["config"];
exports.load = function(config) {
  //init server 
}

loads packages from a directory

loads all the packages

all the loaded packages

exports.require = [ "package.name", ["multiple.packages.*"], /package.to.match/ ];
exports.load    = function (singlePackagemultiplePackagesanotherSinglePackageoptionalSyncNext) {
  
};