打字稿和电子出口未定义

时间:2019-02-10 17:36:15

标签: typescript electron commonjs

我正在尝试运行我的简单电子应用程序。我将Typescript用作可编译为JavaScript的开发语言。当我运行该应用程序时,出现以下错误:

ReferenceError: exports is not defined[Learn More]
file:///Users/ahmet/Documents/JumbleUp-Desktop/dist/Login/Login.js:5
exports.__esModule = true;

我的login.ts文件看起来像这样

    import firebase from "firebase";

firebase.auth().onAuthStateChanged(function(user) {
    if (user) {
        location.replace("index.html");
    } else {
        location.replace("login.html");
    }
  });
function login() {
    const userEmail = (document.getElementById("inputEmail") as HTMLInputElement).value;
    const userPassword = (document.getElementById("inputPassword") as HTMLInputElement).value;

    firebase.auth().createUserWithEmailAndPassword(userEmail, userPassword).catch(function(error) {
        // Handle Errors here.
        var errorCode = error.code;
        var errorMessage = error.message;
        // ...

        window.alert("Alert : " + errorMessage);
      });
}

这是我的tsconfig文件

{
    "compilerOptions": {
      "module": "commonjs",
      "noImplicitAny": true,
      "sourceMap": true,
      "esModuleInterop": true,
      "outDir": "dist",
      "baseUrl": ".",
      "paths": {
        "*": ["node_modules/*"]
      }
    },
    "include": [
      "src/**/*"
    ]
  } 

2 个答案:

答案 0 :(得分:7)

我遇到了同样的问题。对我来说,问题不在于文件的编译方式,而是在于它们如何包含在index.html项目中。

更改:

<script src="./main.js"></script>

<script>
   require("./main.js")
</script>

在index.html

为我解决了

答案 1 :(得分:-1)

我认为您应该将“主要”代码与“渲染器”代码分开。在我看来,类似于客户端和服务器端代码。

无论如何,解决方案...首先,我正在使用以下版本:

  • 节点12.19.0
  • 电子^ 10.1.5
  • TypeScript ^ 4.0.3

我已经在任何其他“渲染器端”代码之前添加了下一行(对于我来说,在我的preloader.ts中):

global.exports = {};

此外,我在webPreferences的{​​{1}}属性中添加了下一个属性:

BrowserWindow

它看起来像这样:

nodeIntegration: true

我的const mainWindow = new BrowserWindow({ height: 600, width: 800, webPreferences: { preload: path.join(__dirname, "preload.js"), nodeIntegration: true // Enables module imports... &%@$! } }); ,以防万一:

tsconfig.json

您也可以在“渲染器”端尝试以下操作:

{
  "compilerOptions": {
    "target": "es6",
    "module": "commonjs",
    "outDir": "dist",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}
相关问题