簡體   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