Giter Site home page Giter Site logo

moduleconfig's Introduction

moduleconfig

loading configuration files for node modules. If you are building a node module that needs a configuration file, that the user of the module creates, you have come to the right place.

Build Status

NPM version

Dependency Status

You are building a module MyModule with a user config file

The module MyModule has a configuration, by convention this configuration specified in in the file config.js.

  • In project Main the module MyModule is required.
  • In project Main the module ExternalModule is also required.
  • ExternalModule also requires MyModule

This means that when the module ExternalModule uses MyModule the configuration should be loaded from Main/node_modules/ExternalModule/config.js, and when the project Main uses the module MyModule the configuration should be loaded from Main/config.js

Problem

Because Main and ExternalModule both require the same version of MyModule. npm only installs MyModule in Main/node_modules/MyModule. MyModule is not installed in Main/node_modules/ExternalModule/node_modules/MyModule.

Therefore the require-ing MyModule from the code ExternalModule in does not load the correct config. It loads the config from Main/config.js and not from Main/node_modules/ExternalModule/config.js as it should. The Main projects config overrides the ExternalModule config.

Solution

Use moduleconfig in the module MyModule

MyModule.js is the main file pointed to by the package.json in the MyModule module

var moduleConfig = require("moduleconfig");
module.exports = moduleConfig(["config.js"], function(configFilePath){
	return instantiateMyModulefromConfig(require(configFilePath));
});

You will have to implement the instantiateMyModulefromConfig function yourself.

This will mean that this:

var myModule = require("MyModule");

will return an myModule based on Main/node_modules/ExternalModule/config.js when MyModule is required from a javascript file anywhere in the path Main/node_modules/ExternalModule/...

and it will return an myModule based on Main/config.js when MyModule is required by code in Main.

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.