Как вы не используете модуль в node.js при использовании Createrequire?Javascript

Форум по Javascript
Anonymous
Как вы не используете модуль в node.js при использовании Createrequire?

Сообщение Anonymous »

Учитывая проект esm node.js (package.json содержит «Тип»: «Модуль» ), я хочу потребовать файла, а затем удалить его кэш и потребовать его снова, чтобы его источник снова выполнялся.

Код: Выделить всё

console.log('in mod.ts')
< /code>
testcase.js
import { createRequire } from 'module'
const require = createRequire(import.meta.url)

require('./mod.js')

const key = Object.keys(require.cache)[0]
delete require.cache[key]

require('./mod.js')
< /code>
There are ways to do this in a CJS project such as this answer, but I can't figure out how to do it in an ESM project.
I expected to see the console log printed twice, but it only prints once.
Is there a way to successfully uncache a module and reload its body in this test case?

Подробнее здесь: https://stackoverflow.com/questions/795 ... aterequire

Вернуться в «Javascript»