electron Typescript找不到已安装的模块

wz3gfoph  于 2022-12-08  发布在  Electron
关注(0)|答案(2)|浏览(258)

我试图构建一个在带有Typescript的电子上运行的客户端,但是,我从request.ts文件中得到错误。

Electron Typescript quickstart尝试导入electron时,我从Electron Typescript quickstart获得的main.ts上也发生了同样的事情。
tsconfig.json的配置如下:

{
  "compilerOptions": {
    "module": "ES2015",
    "noImplicitAny": true,
    "sourceMap": true,
    "outDir": "dist",
    "baseUrl": ".",
    "allowSyntheticDefaultImports": true,
    "types": [ "node" ],
    "paths": {
      "*": ["node_modules/*"]
    }
  },
  "include": [
    "src/**/*"
  ]
}

我确保baseUrl指向node-modules所在的目录,因此我确信编译器可以找到node-modules
为了确保axioselectron已安装,下面是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 --no-sandbox ./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": {
    "@types/axios": "^0.14.0",
    "@types/electron": "^1.6.10",
    "@types/jquery": "^3.5.14",
    "@types/node": "^17.0.42",
    "@typescript-eslint/eslint-plugin": "^4.33.0",
    "@typescript-eslint/parser": "^4.33.0",
    "electron": "^18.2.3",
    "eslint": "^7.32.0",
    "typescript": "^4.7.2"
  },
  "dependencies": {
    "axios": "^0.27.2",
    "jquery": "^3.6.0"
  }
}

当我将module指定为commonjs时,我可以让它运行,但也会出现错误,因为tsc将import语句编译为浏览器不支持的require
我尝试使用tspath,但无法在其中运行,因为我使用的是Windows 11。我也尝试给出相对路径,但最后出现错误,称这些文件“没有找到声明”。我真的很感谢有关如何解决此问题的任何提示。提前感谢。

ebdffaop

ebdffaop1#

我唯一能帮助你的方法就是让你看到我在每个项目中使用的tsconfig.json。试着把它复制并粘贴到你的项目中。

{
  "compilerOptions": {
    "target": "es5",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "typeRoots": [
      "node_modules/@types",
      "src/types"
    ],
    "jsx": "react-jsx"
  },
  "include": [
    "src"
  ]
}
lbsnaicq

lbsnaicq2#

TypeScript上的dotenv模块也遇到了同样的问题。Screenshot with the error TypeScript识别该模块的解决方案是在ts.config文件中将moduleResolution稳定为"moduleResolution": "node"ts.config gile一旦您稳定了此属性,错误应该就会消失。
由于我的项目刚刚开始开发,所以我对TypeScript使用了最小的配置,但我相信我的ts.config文件会有所帮助:

{ //ts.config
 "compilerOptions": {
   "target": "es2016",
   "module": "ES6",
   "rootDir": "./src",
   "moduleResolution": "node",
   "outDir": "./dist",
   "esModuleInterop": true,
   "forceConsistentCasingInFileNames": true,
   "skipLibCheck": true
 }
}

相关问题