21
loading...
This website collects cookies to deliver better user experience
node file.js
then you'll need to use the const my_const = require('./some_file')
convention below.module.exports
so that one file could allow another file to access its functions and variables. In our current sprint, one of our boilerplate project files uses exports
instead. What's the difference?//anyFile.js
console.log(module.exports === exports)
>>true
module.exports
in some cases and exports
in another?exports
to other objects, but you know what we call those references? Rabbit holes. exporter.js
and importer.js
:\\exporter.js
var demoFunc = function(){
console.log('it works!')
}
var demoVar = 'learning is FUNdamental'
module.exports.demoVar = demoVar
module.exports.demoFunc = demoFunc
\\importer.js
const imported = require('./exporter.js');
console.log(imported.demoVar)
imported.demoFunc()
>learning is FUNdamental
>it works!
module.exports
objects in exporter.js
is made available directly by require('./exporter.js')
in importer.js
.require('./exporter.js'); = module.exports
.module.exports.whatever
,require('./exporter.js').WHATEVER
.const demo = require('./exporter.js')
demo.WHATEVER
.21