【问题标题】:module.export is exporting object instead of string stored in variablemodule.export 正在导出对象而不是存储在变量中的字符串
【发布时间】:2021-02-09 01:28:15
【问题描述】:

当我使用 module.exports 并在末尾添加一个字符串时,它会导出并在字符串中正确显示,但是当我导出变量时,它会返回一个对象 {}。

这是 export.js 文件中的代码

    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
}

这里myFunction执行onclick事件。

这是导入文件夹中的代码,用于在控制台中注销一个空对象。

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

【问题讨论】:

  • 太糟糕了。你想用那个归档什么?
  • 我正在尝试导出单个变量的值或在另一个文件中使用该变量,您能告诉我正确的方法吗?
  • @Marc 我正在尝试导出单个变量的值或在另一个文件中使用该变量,您能告诉我正确的方法吗?

标签: javascript module electron node-modules module.exports


【解决方案1】:

对您的问题的简短回答是,模块通常在首次“需要”后被缓存。在节点中有一个完整的require.cache 系统,您可以阅读它。由于我看到您使用的是document,因此我怀疑这些文件是通过 webpack 之类的方式为浏览器捆绑的。在这种情况下,据我所知,没有办法在不真正弄乱 webpack 内部的情况下做到这一点。

但更简短的答案是:不要那样做。无论你想做什么,这都不是办法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-17
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    相关资源
    最近更新 更多