【问题标题】:Typescript cannot find modules that are already installedTypescript 找不到已安装的模块
【发布时间】:2022-10-20 08:16:04
【问题描述】:

我正在尝试使用 Typescript 构建一个在 Electron 上运行的客户端,但是,我从 request.ts 文件中收到错误。

当我尝试导入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"
  }
}

当我将模块指定为commonjs 时,我可以让它运行,但我也会收到错误,因为tsc 将导入语句编译为浏览器不支持的require

我尝试使用tspath,但我无法在其中运行,因为我使用的是 Windows 11。我也尝试提供相对路径,但最终出现错误,提示“没有找到这些文件的声明”。我非常感谢有关如何解决此问题的任何提示。提前致谢。

【问题讨论】:

    标签: typescript electron


    【解决方案1】:

    我可以帮助您的唯一方法是让您看到我在每个项目中使用的 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"
      ]
    }
    

    【讨论】:

    • 这有助于消除导入错误,但现在我在 try-catch 块中遇到错误,说 Object is of type 'unknown' console.log('error message: ', error.message);
    • 根据这个*.com/questions/60151181/…你有两个选择:1.} catch(e) { if (e instanceof Error) { console.log(e.message) } }2.在tsconfig.json的“严格”下添加这个"useUnknownInCatchVariables": false
    • 谢谢!但是现在也有消息说'resourceCategory.ts' cannot be compiled under '--isolatedModules' because it is considered a global script file. Add an import, export, or an empty 'export {}' statement to make it a module.。从配置文件中删除此选项会导致 main.js import { app, browserWindow } from "electron"; cannot use import statement outside a module
    • 我帮不了你,我需要看一些代码才能理解。只需将答案标记为正确(它目前回答了您的问题),然后针对您当前面临的问题(包含详细信息、代码屏幕)打开另一张工单,我可以轻松地为您提供帮助
    • 会做。感谢您到目前为止的修复:)
    【解决方案2】:

    我在 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",
       "outDir": "./dist",
       "esModuleInterop": true,
       "forceConsistentCasingInFileNames": true,
       "skipLibCheck": true
     }
    }
    

    【讨论】: