繁体   English   中英

Electron 和 Typescript - 未捕获的引用错误:未定义导出

[英]Electron and Typescript - Uncaught ReferenceError: exports is not defined

我有一个用 typescript 编写的 electron 应用程序。这是最少的代码:

someModule.ts

function someFunction() {}

export default someFunction;

main.ts

import someFunction from "./someModule";

someFunction();

index.html

...

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

...

index.ts (以npm start开头的包条目):

import { BrowserWindow, app } from "electron";

const createWindow = () => {
    const win = new BrowserWindow({
        webPreferences: {
            nodeIntegration: true,
            contextIsolation: false,
        }
    });

    win.loadFile("./index.html");
}

app.whenReady().then(() => {
    createWindow();
});

app.on("window-all-closed", () => {
    if(process.platform !== "darwin") app.quit();
})

tsconfig.json

{
  "compilerOptions": {
    "target": "ES2022",
    "lib": ["DOM"], 
    "module": "CommonJS",
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true, 
    "skipLibCheck": true                                 
  }
}

当我运行应用程序时(通过npx tsc && node index.js命令(即npm start )),electron window 中的控制台出现错误:

Uncaught ReferenceError: exports is not defined
    at main.js:5:23

我怎样才能解决这个问题?

通过在index.html文件中添加以下内容解决了该问题:

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

而不是<script src="main.js"></script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM