【问题标题】:Why I'm getting "exports is not defined" on my renderer process?为什么我的渲染器进程出现“未定义导出”?
【发布时间】:2020-10-01 04:31:52
【问题描述】:

我正在将现有的电子项目迁移到打字稿电子项目,以熟悉它。但是,当我尝试运行电子应用程序时,我收到此错误:Uncaught ReferenceError: exports is not defined at renderer.js:2

一旦我的 tsconfig.json 是这样的,我就不明白了:

{
  "compilerOptions": {
    "target": "es5",
    "module": "commonjs",
    "outDir": "./dist/src",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true
  }
}

我的启动脚本:"start": "tsc && electron ./dist/src/main"

我搜索了一下,发现了一个电子打字稿样板 here 并搜索了一些使用它的项目,所有这些项目都使用了导入/导出并且没有引发任何错误。

解决方法

在 renderer.js 上,我注释了给我错误的部分,所以新文件是:

"use strict";
//Object.defineProperty(exports, "__esModule", { value: true });
var electron_1 = require("electron");
var suBtn = document.querySelector("input#sb");
var result = document.querySelector("div#result");
suBtn.addEventListener("click", function (event) {
  event.preventDefault();
  var pathElement = document.querySelector("input#file");
  if (pathElement.files !== null) {
    var path = pathElement.files[0].path;
    electron_1.ipcRenderer.on("duration", function (event, duration) {
      result.innerHTML = "Video is " + duration + " seconds!";
    });
    electron_1.ipcRenderer.send("video:submit", path);
  }
});

渲染器.ts:

import { ipcRenderer as ipc } from "electron";

const suBtn = document.querySelector("input#sb") as HTMLInputElement;

const result = document.querySelector("div#result") as HTMLDivElement;

suBtn.addEventListener("click", (event) => {
  event.preventDefault();

  const pathElement = document.querySelector("input#file") as HTMLInputElement;

  if (pathElement.files !== null) {
    const path = pathElement.files[0].path;
    ipc.on("duration", (event, duration) => {
      result.innerHTML = `Video is ${duration} seconds!`;
    });
    ipc.send("video:submit", path);
  }
});

所以,我每次都需要手动注释这行?

【问题讨论】:

  • 编辑编译好的打字稿肯定是错的。请发布导致错误的打字稿代码。
  • 我知道是错的,但只有这样应用才能工作。错误代码是在 renderer.js 文件中注释的代码。
  • 当然可以,但是您可以编辑问题并添加renderer.ts 文件的(相关部分)吗?编译的javascript没有帮助
  • 啊,好的,当然!我会补充的。

标签: javascript node.js typescript electron


【解决方案1】:

这看起来像是 typescript 上的一个已知问题:https://github.com/microsoft/TypeScript/issues/30718

据我了解,当您有一个目标 commonjs 和一个导入但不导出的打字稿文件(对于渲染器文件来说是正常的)时,可能会发生这种情况。

有些人像你一样做了同样的事情,删除了那条线,并通过观察者自动化了事件。

我还看到有一个拉取请求引入了一个新的配置标志,以便您可以禁止该行,但自去年 9 月以来一直处于未决状态。

cmets 提到了另一个“hack”,我会在脚本上这样做:只需自己添加一个变量“exports”。它必须是 varconstlet 不起作用:

  // see: https://github.com/microsoft/TypeScript/issues/30718#issuecomment-479609634
  var exports:any = {};

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    相关资源
    最近更新 更多