Coder Social home page Coder Social logo

requireg's Introduction

requireg Build Status NPM version

Require and resolve global modules in node/io.js like a boss

Differences with require()

requireg tries to find modules in global locations which are not natively supported by the node.js module resolve algorithm.

Supported locations:

  • $HOME/node_modules (instead of $HOME/.node_modules)
  • $HOME/node_libraries (instead of $HOME/.node_libraries)
  • $HOME/node_packages (specific of requireg)
  • $PREFIX/lib/node_modules (instead of $PREFIX/lib/node)
  • $NODE_MODULES (use the specific modules path environment variable)

Resolution priority

  1. Resolve via native require()
  2. User home directory ($HOME or %USERPROFILE%)
  3. Node installation path
  4. $NODE_MODULES (can have different multiple paths, semicolon separated)
  5. Common operative system installation paths

Installation

$ npm install requireg --save[-dev]

Usage

Load global modules

var requireg = require('requireg')
// require a globally installed package
var npm = requireg('npm')

Resolve module path

var modulePath = requireg.resolve('npm')
// returns '/usr/local/lib/node_modules/npm/lib/npm.js'

Globalize it

require('requireg').globalize()

Now it is globally available from any source file

var globalModule = requireg('npm')

Module not found

requireg maintains the same behavior as the native require(). It will throw an Error exception if the module was not found

Considerations

  • Require global modules in node.js are considered anti-pattern. Note that you can experiment unreliability or inconsistency across different environments. I hope you know exactly what you do with requireg
  • Only node packages installed with NPM are supported (which means only standardized NPM paths are supported)

Wish list

  • Custom environment variable with custom path to resolve global modules
  • Possible configuration object (force to use only global modules...)

License

Released under MIT license

requireg's People

Contributors

h2non avatar eush77 avatar

Watchers

James Cloos avatar  avatar

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.