
Security News
Deno 2.2 Improves Dependency Management and Expands Node.js Compatibility
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
enhanced-require
Advanced tools
Enhance the require function in node.js with support for loaders which preprocess files and really async require (AMD). Enables Hot Code Replacement.
More features for node.js require.
require.ensure
require
, define
(from require.js)require.context
Asynchron require functions are really async. They do not use the sync node.js require, but use a async resolving and async readFile.
var myRequire = require("enhanced-require")(module, {
// options
recursive: true // enable for all modules recursivly
// This replaces the original require function in loaded modules
});
// startup your application
myRequire("./startup");
Than you can use them:
// use loaders
var fileContent = require("raw!"+__filename);
// use loaders automatically
var template = require("./my-template.jade");
var html = template({content: fileContent});
// use require.context
var directoryRequire = require.context("raw!./subdir");
var txtFile = directoryRequire("./aFile.txt");
// use require.ensure
require.ensure(["./someFile.js"], function(require) {
var someFile = require("./someFile.js");
});
// use AMD define
require.define(["./aDep"], function(aDep) {
aDep.run();
});
// use AMD require
require(["./bDep"], function(bDep) {
bDep.doSomething();
});
require("enhanced-require")(module, {
recursive: true, // enable for all modules
hot: true, // enable hot code replacement
watch: true // watch for changes
})("./startup");
For hot code reloading you need to follow the hot code reloading spec.
var er = require("enhanced-require");
it("should read the config option", function(done) {
var subject = er(module, {
recursive: true,
substitutions: {
// specify the exports of a module directly
"../lib/config.json": {
"test-option": { value: 1234 }
}
},
substitutionFactories: {
// specify lazy generated exports of a module
"../lib/otherConfig.json": function(require) {
// export the same object as "config.json"
return require("../lib/config.json");
}
}
})("../lib/subject");
var result = subject.getConfigOption("test-option");
should.exist(result);
result.should.be.eql({ value: 1234 });
});
{
recursive: false,
// replace require function in required modules with enhanced require method
resolve: {
// ...
// see enhanced-resolve
// https://github.com/webpack/enhanced-resolve
},
substitutions: {},
substitutionFactories: {},
// See above
// Replace modules with mocks
// keys are resolved and have to exist
amd: {},
// The require.amd object
enhanced: {},
// The require.enhanced object
loader: {},
// additional stuff in the loaderContext
hot: false,
// enable hot code replacement
watch: false,
// Watch for file changes and issue hot replacement
watchDelay: 400,
// Time to summarize changes for watching
}
Copyright (c) 2012 Tobias Koppers
FAQs
Enhance the require function in node.js with support for loaders which preprocess files and really async require (AMD). Enables Hot Code Replacement.
The npm package enhanced-require receives a total of 194 weekly downloads. As such, enhanced-require popularity was classified as not popular.
We found that enhanced-require 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.
Security News
Deno 2.2 enhances Node.js compatibility, improves dependency management, adds OpenTelemetry support, and expands linting and task automation for developers.
Security News
React's CRA deprecation announcement sparked community criticism over framework recommendations, leading to quick updates acknowledging build tools like Vite as valid alternatives.
Security News
Ransomware payment rates hit an all-time low in 2024 as law enforcement crackdowns, stronger defenses, and shifting policies make attacks riskier and less profitable.