Exports vs Module.Exports (Node)
Tags: problemsolvingprocess
Published: 2021-07-10 15:18:31

*Edit: I'm prepending here a little section on another system of imports and exports.*

This post is about imports/exports in Node. Another way to think about it is, if you run the command `node file.js` then you'll need to use the `const my_const = require('./some_file')` convention below.

But if you're running a non-node library like React, you'll import a little differently. The required import/export statements for these situations are described adequately in the [MDN docs](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import).

---
*Ok back to our regularly scheduled content*

We stared using Node last week, and as I wrote in an earlier post, it works a bit differently from the JS environment in-browser. Notably, the way files interact and gain access to each others' properties (this is a sentence fragment but feels right anyway).

In an earlier sprint, we used `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?

The answer to this question unfolded only after really careful study and many late nights working by candlelight. Read on only if you're ready to embark on a similar journey.

##Exports IS Module.Exports

That's it. That's the answer. No more journey.

Here I'll show you.

```javascript
//anyFile.js

console.log(module.exports === exports)
>>true
```

True! Mystery solved.

So with that, why are they both used? Why does out boilerplate code use `module.exports` in some cases and `exports` in another?

I'm not sure! But what's important now is that I have a working knowledge of this tool, and that's exactly what I was looking for. I did come across some references to cases where other developers have reassigned `exports` to other objects, but you know what we call those references? *Rabbit holes*. 

Can I use exports? Yes. Can I use module.exports? Yes. Is that good enough to continue with my work? Yes.

##Ok so what is module.exports?

If you want a really nice explanation, read here: https://www.freecodecamp.org/news/require-module-in-node-js-everything-about-module-require-ccccd3ad383/.

If you like demos, I've got you covered.

Let's take two files in the same directory: `exporter.js` and `importer.js`:

```javascript
\\exporter.js

var demoFunc = function(){
  console.log('it works!')
}

var demoVar = 'learning is FUNdamental'

module.exports.demoVar = demoVar
module.exports.demoFunc = demoFunc
```

```javascript
\\importer.js

const imported = require('./exporter.js');

console.log(imported.demoVar)
imported.demoFunc()

>learning is FUNdamental
>it works!
```

What happens is that the `module.exports` objects in `exporter.js` is made available directly by  `require('./exporter.js')` in `importer.js`.

You can think of it like this:

`require('./exporter.js'); = module.exports`.

So if we assign WHATEVER like so: `module.exports.whatever`,

then that is available in another file like so: `require('./exporter.js').WHATEVER`.

And if we set that 'require' statement to a variable: `const demo = require('./exporter.js')`

then we can now access our property via: `demo.WHATEVER`.