【问题标题】:Electron - Typescript - Cannot find module电子 - 打字稿 - 找不到模块
【发布时间】:2022-01-06 00:14:27
【问题描述】:

尝试使用从 electron-quick-start-typescript 项目生成的 typescript 构建我的电子应用程序。我添加了一个名为 auth.ts 的附加模块,当我启动应用程序时无法识别该模块。我正在尝试在 renderer.ts 中引用它

import { myfunction } from './auth'

但是我可以看到它正在转换为 js。什么可能导致此问题?为什么我的应用程序看不到我的新模块?

如果有帮助,这里还有我的 package.json 文件。

{
  "name": "electron-quick-start-typescript",
  "version": "1.0.0",
  "description": "A minimal Electron application written with Typescript",
  "scripts": {
    "build": "tsc",
    "watch": "tsc -w",
    "lint": "eslint -c .eslintrc --ext .ts ./src",
    "start": "npm run build && electron ./dist/main.js"
  },
  "repository": "https://github.com/electron/electron-quick-start-typescript",
  "keywords": [
    "Electron",
    "quick",
    "start",
    "tutorial",
    "demo",
    "typescript"
  ],
  "author": "GitHub",
  "license": "CC0-1.0",
  "devDependencies": {
    "@typescript-eslint/eslint-plugin": "^4.33.0",
    "@typescript-eslint/parser": "^4.33.0",
    "electron": "^16.0.2",
    "eslint": "^7.32.0",
    "typescript": "^4.5.2"
  },
  "dependencies": {
    "node-fetch": "^2.6.1"
  }
}

【问题讨论】:

    标签: javascript typescript electron


    【解决方案1】:

    找到了答案。对于其他有同样问题的人,这解决了问题 -

    确保在 main.js 中启用了 nodeIntegration

    webPreferences: {
          nodeIntegration: true,
          preload: path.join(__dirname, "preload.js"),
        },
    

    索引.html

    替换:

    <script src="./dist/renderer.js"></script>
    

    与:

    <script>
        require("./dist/renderer.js");
    </script>
    

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 1970-01-01
      • 2021-01-07
      • 2013-12-30
      • 2018-01-03
      • 2017-05-05
      • 2018-06-27
      相关资源
      最近更新 更多