Skip to content

Instantly share code, notes, and snippets.

@magnetikonline
Last active May 19, 2023 05:38
Show Gist options
  • Save magnetikonline/8fa2aef469b8f6c2e516 to your computer and use it in GitHub Desktop.
Save magnetikonline/8fa2aef469b8f6c2e516 to your computer and use it in GitHub Desktop.
Node.js module dependency injection pattern.

Node.js module dependency injection pattern

The module

'use strict';

let config = {};

// inject dependencies
module.exports = (_config) => {
  config = _config;
  return exports;
};

exports.methodOne = (param01) => {
  return config[param01] !== undefined;
};

Using module

'use strict';

const config = new Config(),
  myModule = require('./path/to/module.js')(config);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment