Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/import-fresh
Import a module while bypassing the cache
https://github.com/sindresorhus/import-fresh
Last synced: 1 day ago
JSON representation
Import a module while bypassing the cache
- Host: GitHub
- URL: https://github.com/sindresorhus/import-fresh
- Owner: sindresorhus
- License: mit
- Created: 2014-07-19T22:21:27.000Z (over 10 years ago)
- Default Branch: main
- Last Pushed: 2024-06-06T06:12:23.000Z (6 months ago)
- Last Synced: 2024-10-29T12:58:38.430Z (about 1 month ago)
- Language: JavaScript
- Homepage:
- Size: 23.4 KB
- Stars: 284
- Watchers: 8
- Forks: 25
- Open Issues: 10
-
Metadata Files:
- Readme: readme.md
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-nodejs-cn - import-fresh - 调用模块时绕开缓存 (包 / 其他)
- awesome-nodejs - import-fresh - Import a module while bypassing the cache - ★ 146 (Miscellaneous)
- awesome-node - import-fresh - Import a module while bypassing the cache. (Packages / Miscellaneous)
- awesome-nodejs-cn - import-fresh - Import a module while bypassing the cache. (目录 / 其他)
README
# import-fresh
> Import a module while bypassing the [cache](https://nodejs.org/api/modules.html#modules_caching)
Useful for testing purposes when you need to freshly import a module.
## ESM
For ESM, you can use this snippet:
```js
const importFresh = moduleName => import(`${moduleName}?${Date.now()}`);const {default: foo} = await importFresh('foo');
```**This snippet causes a memory leak, so only use it for short-lived tests.**
## Install
```
$ npm install import-fresh
```## Usage
```js
// foo.js
let i = 0;
module.exports = () => ++i;
``````js
const importFresh = require('import-fresh');require('./foo')();
//=> 1require('./foo')();
//=> 2importFresh('./foo')();
//=> 1importFresh('./foo')();
//=> 1
```## Related
- [clear-module](https://github.com/sindresorhus/clear-module) - Clear a module from the import cache
- [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path
- [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory
- [import-lazy](https://github.com/sindresorhus/import-lazy) - Import modules lazily