awesome-cheatsheets icon indicating copy to clipboard operation
awesome-cheatsheets copied to clipboard

Module / require example

Open bahmutov opened this issue 6 years ago • 0 comments

https://github.com/LeCoupa/awesome-cheatsheets/blob/master/backend/node.js#L77 should be more like

const foo = require('./subfolder/foo.js')
console.log('foo "%s"', foo) // "foo"

// now grab the "module" wrapper around './foo.js'
const fooModule = require.cache[require.resolve('./subfolder/foo.js')]

console.log('foo module', fooModule)
// load module ./bar relative to ./foo
// real path to './bar' is './subfolder/bar'
const bar = fooModule.require('./bar')
console.log('bar "%s"', bar) // "bar"

bahmutov avatar Feb 08 '18 18:02 bahmutov