'how to import require.cache and require.resolve
- Since i'm using module type in package.json or
importinstead of require. - how can i import require.cache and require.resolve
- i'm trying to reload a file and update changes made.
// error: require is not defined
delete require.cache[require.resolve(`./index.js`)];
- I tried till now
// This runs with no issues but doesn't update the changes.
import { resolve } from 'node:path';
import { createRequire } from 'node:module';
const require = createRequire(import.meta.url);
delete require.cache[resolve(`./index.js`)];
- without path
const require = createRequire(import.meta.url);
delete require.cache[require.resolve(`./index.js`)];
- But this doen't update the changes made in
index.js
Expected Result
- If i reload specific file and it should update the changes made to file (
index.js).
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
