import-fresh
's direct dependencies. Data on all dependencies, including transitive ones, is available via CSV download.Name | Version | Size | License | Type | Vulnerabilities |
---|---|---|---|---|---|
caller-path | 2.0.0 | 1.44 kB | MIT | prod | |
resolve-from | 3.0.0 | 2.04 kB | MIT | prod |
The import-fresh
module in JavaScript offers an opportunity to import another module while bypassing the cache. This can be extremely useful, particularly when testing, as it allows users to freshly import a module without any residual cache variables or data.
The implementation and use of the import-fresh
module is very simple. After installing the package using npm install import-fresh
, you can require it in your script with const importFresh = require('import-fresh');
. Here's how it works in practice:
// In your foo.js file, you might have something like this:
let i = 0;
module.exports = () => ++i;
// Then in your main file:
const importFresh = require('import-fresh');
require('./foo')(); //=> 1
require('./foo')(); //=> 2
importFresh('./foo')(); //=> 1
importFresh('./foo')(); //=> 1
As the above example demonstrates, the same module, when imported normally, maintains the count (i
) in the cache. So, every time the module exports function is called, the count increases. However, when the same module is imported using the import-fresh
module, the cache is bypassed, resulting in the count resetting to its original value each time the function is called.
The import-fresh documentation and further details concerning its functionality and usage can be found on its official GitHub page here. This page also discusses how import-fresh can be beneficial for enterprise-level applications, through a Tidelift Subscription, which provides commercial support and maintenance for this and thousands of other open-source dependencies.