Odasaku
Odasaku

Reputation: 367

module.export is exporting object instead of string stored in variable

When I use module.exports and add a string at the end it exports and shows correctly in the string but when I export a variable instead it returns an object {}.

This is the code from the export.js file

    clientList = document.getElementById("client-list")
    aLink = document.getElementsByClassName('a-link')
    var exVariable;
    function myFunction(event) {
        event.target.setAttribute('href', 'clientinfo.ejs')
        var variable = event.target.innerText
        exVariable = variable
        console.log(exVariable)
        return exVariable  
}    
if (exVariable != null) {
    module.exports = exVariable
}

Over here myFunction executes onclick event.

And this is the code from the import folder which logging out an empty object in the console.

var clientFolder = require('./export.js')
console.log(clientFolder)

Upvotes: 0

Views: 592

Answers (1)

johncomposed
johncomposed

Reputation: 99

The short answer to your question is modules are generally cached after they're first "require"d. In node there is a whole require.cache system that you can read up about. Since I see you're using document, I suspect these files are being bundled for the browser via something like webpack. In that case, to the best of my knowledge there isn't a way to do that without really messing with webpack internals.

But the even shorter answer is: don't do that. Whatever you're trying to do, this is not the way to do it.

Upvotes: 1

Related Questions