Created
June 30, 2020 20:35
-
-
Save adeelibr/5009a021235fd709c5381df8c9ece536 to your computer and use it in GitHub Desktop.
require.cache
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
message: 'I am another.js', | |
say: function () { | |
return 'Hi! how are you :)' | |
}, | |
end: function () { | |
return '-----------------' | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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