up-require
v3.0.2
Published
Require package module from highest or list of module.
Downloads
31
Maintainers
Readme
up-require
Require package module from highest or list of module.
base on
for more desc see here
requireFromParentUp(id: string, startModule?: NodeModule)
{:.language-ts} => parent-requirerequireFromTopParent(id: string, startModule?: NodeModule)
{.ts} => top-requiregetModuleByExports(exports)
=> which-module
diff
- support NodeJS.ErrnoException, Error object will has
code='MODULE_NOT_FOUND'
.
so we can know when error happen is can't found module or it has other error - split function, so u can custom it
- other api see here
Install
npm install up-require
demo
structure
root
- index
- node_modules/
chai@3
- packages/
sub1_pkg/
- node_modules/
chai@1
- index
- node_modules/
sub2_pkg/
- node_modules/
chai@2
- index
sub3_of_sub2_pkg/
- node_modules/
chai@1
- index
- node_modules/
- node_modules/
base use
requireFromTopParent<T = any>(id: string, startModule?: NodeModule)
=> top-requirerequireFromParentUp<T = any>(id: string, startModule?: NodeModule)
=> parent-requiregetModuleByExports
=> which-module
alias name
upRequire
= requireUp
= requireFromTopParent
import requireFromTopParent, { getModuleByID, requireFromParentUp } from 'up-require';
import { requireFromTopParent, getModuleByID, requireFromParentUp } from 'up-require';
const requireFromTopParent = require('up-require').requireFromTopParent;
when use in sub1_pkg/index
require('chai') => chai@1
requireFromParentUp('chai', module) => chai@3
requireFromTopParent('chai', module) => chai@3
requireFromTopParent('chai') => chai@3
when use in sub3_of_sub2_pkg/index
require('chai') => chai@1
requireFromParentUp('chai', module) => chai@2
requireFromTopParent('chai', module) => chai@3
requireFromTopParent('chai') => chai@3
getModuleByID(id: string, requireIfNotExists?: boolean, req = require)
console.log(`only return when chai is required`, getModuleByID('chai'));
console.log(`when chai is not required , will require it`, getModuleByID('chai', true));
Credits
- Creator of original Parent Require Jared Hanson
- Lukasz Sielski
License
Copyright (c) 2014 Lukasz Sielski <http://lukaszsielski.pl/> Copyright (c) 2013 Jared Hanson <http://jaredhanson.net/>