Skip to content

Instantly share code, notes, and snippets.

@adeelibr
Created June 30, 2020 20:35
Show Gist options
  • Save adeelibr/5009a021235fd709c5381df8c9ece536 to your computer and use it in GitHub Desktop.
Save adeelibr/5009a021235fd709c5381df8c9ece536 to your computer and use it in GitHub Desktop.
require.cache
const path = require('path')
const libPath = path.join(require.resolve('./my-library/another'), '..')
// console.log('libPath :: ', libPath)
const anotherPath = path.join(libPath, 'another')
const another = require(anotherPath)
// console.log('anotherPath ::', anotherPath)
// console.log('another ::', another)
// console.log(require.cache)
// console.log(require.cache[require.resolve(anotherPath)])
require.cache[require.resolve(anotherPath)].exports = {
...another,
message: 'I am superman'
}
// execute my-libary
require(libPath)
module.exports = {
message: 'I am another.js',
say: function () {
return 'Hi! how are you :)'
},
end: function () {
return '-----------------'
}
}
const another = require('./another')
console.clear()
console.log(another.message)
console.log(another.say())
console.log(another.end())
console.log('i am called from my-libary/index.js')
console.log('I am my-libary')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment