Socket
Book a DemoInstallSign in
Socket

requiring

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

requiring

Requiring modules sync or async, existent or non.

latest
Source
npmnpm
Version
0.0.5
Version published
Weekly downloads
0
Maintainers
1
Weekly downloads
 
Created
Source

requiring Build Status

Main use case is to avoid errors when requiring files from current working directory that may not exist. e.g. You want to build a module that reads a config file from the user's project root (not node_modules/my-module/ directory).

This only works on file & folder modules -not core modules or modules installed in node_modules directory

npm install requiring

npm test

####require a module async

var requiring = require('requiring');

requiring.async('./cool-tool', function(err, mod) {
  if (err !== null) {
    return console.log(err);
  }
  mod.doStuff();
});

// specify a different base directory to look for a module (default is current working directory)
requiring.async('./another-tool', {
  directory: __dirname // `__dirname` returns directory of current executing script.
}, function(err, mod) {
  if (err !== null) {
    return console.log(err);
  }
  mod.doStuff();
});

####require a module sync

var coolTool = requiring.sync('./cool-tool'); // undefined if there is no 'cool-tool'

// pass a default value to return if no module is found
var configFile = requiring.sync('./config-file', {}); //returns {} if there is no 'config-file'

// specify a different base directory to look for a module (default is current working directory)
var anotherTool = requiring.sync('./another-tool', {
  directory: __dirname // path is resolved via `path.resolve()`
});

####other info

This utility attempts to resolve using a full path so requiring('./cool-tool'); turns into something like this requiring('/Users/johndoe/projects/my-project/cool-tool');

To specify a different base directory use the options hash which is passed in as 2nd parameter in both sync and async (see examples) (TO BE CHANGED IN 0.1.0).

It will validate the path before requiring the module. If no file extension is given, it validates the path in this order: ./cool-tool, ./cool-tool.js, ./cool-tool.json, ./cool-took.node

for the case of ./cool-tool This utility will leverage node's module loading pattern and look for ./cool-tool/index.js or the main file defined in ./cool-tool/package.json

requiring a module async is really just validating the path async, then requiring the module sync.

All paths passed to requiring(path) are looked for relative to the current working directory

supports iojs and is backwards compatible with at least node v0.10.0

Keywords

require

FAQs

Package last updated on 15 Jun 2015

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts