
Product
Socket for Jira Is Now Available
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.
require-extended
Advanced tools
Extended require with awesome features!
This package lets you modify how require should work. It adds some cool features like requiring from projects root path or redirecting all fs requires to your custom custom-fs.js module.
npm install require-extended
yarn add require-extended
Import require extended in your entry file.
const requireExtended = require('require-extended')();
You can use require('~/file-from-root.js') at anywhere of your project.
If you want to disable this feature or change the prefix you can pass options at your entry file where you first required require-extended.
const requireExtended = require('require-extended')({
resolveRoot: {
enabled:true,
prefix: '?'
}
});
By default root path is found using the package app-root-path.
If you want to set it to a custom path you can always use setRoot. You can pass the absolute path or relative path. Relative paths will be resolved from the caller file.
const requireExtended = require('require-extended')();
requireExtended.setRoot(myCustomRootPath);
You can return custom variables for require statements using .binding() feature. Bindings define what NodeJs should load for require.
const requireExtended = require('require-extended')();
const fsBinding = requireExtended.binding('fs', {
readFileSync: () => {
//custom implementation
}
});
In this example for any file requires fs module they will receive a custom object. You can use any type of variable as a binding.
.restore().fsBinding.restore();
Now, all require calls to fs will receive the real fs module.
const requireExtended = require('require-extended')();
const jsonBinding = requireExtended.binding('./folder/language.json', {langugues: []});
All files that requires ./folder/language.json will now receive the custom json data.
_load state where NodeJs checks module caches.Mimics are different than bindings, they change how NodeJs finds the file that should be loaded. Mimics are helping NodeJs to navigate between right files.
const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic('fs', 'querystring');
Whenever a file requires fs module now will receive querystring module.
const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic('fs', './custom-fs.js');
Now the files require fs module will receieve your custom module.
const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic('fs', '~/custom-fs.js');
Now the files require fs module will receieve your custom module on your project root.
const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic((path) => {
return path === 'fs'
}, '~/custom-fs.js');
const requireExtended = require('require-extended')();
const fsMimic = requireExtended.mimic('fs', 'querystring');
const querystringMimic = requireExtended.mimic('querystring', 'http');
const http = require('fs');
Whenever a file require('fs') they will receive http module. Because fs -> querystring -> http.
.restore().fsMimic .restore();
require-extendedYou can .binding your third-party libraries to make them return your instances.
const requireExtended = require('require-extended')();
const sinon = require('sinon');
const fsBinding = requireExtended.binding('registerLibrary', sinon.spy());
You can return custom objects for testing. So you can easily assure that your important require lines are not removed by a coworker.
FAQs
Extends require with some awesome features
We found that require-extended demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

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.

Product
Socket for Jira lets teams turn alerts into Jira tickets with manual creation, automated ticketing rules, and two-way sync.

Company News
Socket won two 2026 Reppy Awards from RepVue, ranking in the top 5% of all sales orgs. AE Alexandra Lister shares what it's like to grow a sales career here.

Security News
NIST will stop enriching most CVEs under a new risk-based model, narrowing the NVD's scope as vulnerability submissions continue to surge.