|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
1.1 KB
|
| Line | |
|---|
| 1 | # import-fresh
|
|---|
| 2 |
|
|---|
| 3 | > Import a module while bypassing the [cache](https://nodejs.org/api/modules.html#modules_caching)
|
|---|
| 4 |
|
|---|
| 5 | Useful for testing purposes when you need to freshly import a module.
|
|---|
| 6 |
|
|---|
| 7 | ## ESM
|
|---|
| 8 |
|
|---|
| 9 | For ESM, you can use this snippet:
|
|---|
| 10 |
|
|---|
| 11 | ```js
|
|---|
| 12 | const importFresh = moduleName => import(`${moduleName}?${Date.now()}`);
|
|---|
| 13 |
|
|---|
| 14 | const {default: foo} = await importFresh('foo');
|
|---|
| 15 | ```
|
|---|
| 16 |
|
|---|
| 17 | **This snippet causes a memory leak, so only use it for short-lived tests.**
|
|---|
| 18 |
|
|---|
| 19 | ## Install
|
|---|
| 20 |
|
|---|
| 21 | ```sh
|
|---|
| 22 | npm install import-fresh
|
|---|
| 23 | ```
|
|---|
| 24 |
|
|---|
| 25 | ## Usage
|
|---|
| 26 |
|
|---|
| 27 | ```js
|
|---|
| 28 | // foo.js
|
|---|
| 29 | let i = 0;
|
|---|
| 30 | module.exports = () => ++i;
|
|---|
| 31 | ```
|
|---|
| 32 |
|
|---|
| 33 | ```js
|
|---|
| 34 | const importFresh = require('import-fresh');
|
|---|
| 35 |
|
|---|
| 36 | require('./foo')();
|
|---|
| 37 | //=> 1
|
|---|
| 38 |
|
|---|
| 39 | require('./foo')();
|
|---|
| 40 | //=> 2
|
|---|
| 41 |
|
|---|
| 42 | importFresh('./foo')();
|
|---|
| 43 | //=> 1
|
|---|
| 44 |
|
|---|
| 45 | importFresh('./foo')();
|
|---|
| 46 | //=> 1
|
|---|
| 47 | ```
|
|---|
| 48 |
|
|---|
| 49 | ## Related
|
|---|
| 50 |
|
|---|
| 51 | - [clear-module](https://github.com/sindresorhus/clear-module) - Clear a module from the import cache
|
|---|
| 52 | - [import-from](https://github.com/sindresorhus/import-from) - Import a module from a given path
|
|---|
| 53 | - [import-cwd](https://github.com/sindresorhus/import-cwd) - Import a module from the current working directory
|
|---|
| 54 | - [import-lazy](https://github.com/sindresorhus/import-lazy) - Import modules lazily
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.