【问题标题】:Launching Typescript app in Visual Studio Code throws error "Cannot find module 'electron'"在 Visual Studio Code 中启动 Typescript 应用程序会引发错误“找不到模块‘电子’”
【发布时间】:2016-06-12 10:11:29
【问题描述】:

尝试在 Visual Studio Code(vs code)中启动打字稿应用程序时,我收到错误“找不到模块 'electron'”。我正在尝试启动的项目是 black-screen,我从 github 克隆了它。

以下语句会引发此错误:

import {ipcMain, nativeImage} from "electron";

(在文件https://github.com/shockone/black-screen/blob/master/src/main/Main.ts#l3的第3行)

我可以使用 typescript-compiler (tsc) 编译应用程序并且不会产生错误,并且可以在我期望的文件夹 (src/bin/) 中看到已编译的 javascript。我也可以使用 npm ("npm start") 成功启动应用程序。

以下是相关的项目配置文件:

  1. src/tsconfig.json

    {
        "compilerOptions": {
        "target": "es6",
        "module": "commonjs",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "moduleResolution": "node",
        "experimentalDecorators": true,
        "noEmitOnError": true,
        "pretty": true,
        "jsx": "react",
        "sourceMap": true,
        "outDir": "bin"
      }
    }
    
  2. .vscode/tasks.json 文件 笔记。在终端“tsc --project src --moduleResolution node”中执行等效命令会生成没有错误或警告的转译js代码。

    {
        "version": "0.1.0",
        "command": "tsc",
        "isShellCommand": true,
        "showOutput": "silent",
        "args": ["--project", "src", "--moduleResolution", "node"],
        "problemMatcher": "$tsc"
    }
    
  3. .vscode/launch.json

    {
        "version": "0.2.0",
        "configurations": [
            {
                "name": "Launch Black-Screen",
                "type": "node",
                "request": "launch",
                "program": "${workspaceRoot}/src/main/Main.ts",
                "stopOnEntry": false,
                "cwd": "${workspaceRoot}/src",
                "sourceMaps": true,
                "outDir": "${workspaceRoot}/src/bin"
            }
        ]
    }
    

顺便说一句。项目结构为:

|.vscode/
|-- launch.json
|-- tasks.json
|decorators/
|...
|node_modules/
|-- bin/
|-- abbrev/
|-- acorn/
|README/
|-- <image files>
|src/
|-- bin/
|-- main/
|---- Main.ts
|---- Menu.ts
|...
|-- tsconfig.json
|...
|stylesheets/
|...
|test/
|...
|typings/
|...
|.babelrc
|.gitignore
|.npmrc
|...
|gulfile.bable.js
|package.json
|...

任何帮助将不胜感激:)

【问题讨论】:

  • 你有 Electron 的定义文件 (.d.ts) 吗?没有它,Typescript 语言服务将无法判断它的存在。
  • 事实上,我刚刚注意到你克隆的仓库有一个typings.json - 尝试在项目目录中运行npm install typings -g 然后typings install。
  • 谢谢乔。在我看到 Basarat 的回答后,我检查并看到“typings”目录已经在存储库文件中。我确实尝试过安装类型(npm install -g typings & typings install)。
  • 但是在运行这些命令并重新编译后,我看到了错误 - error TS2300: Duplicate identifier &lt;variable-name&gt; for files "typings/browser/ambient/github-electron/github-electron.d.ts" 和 "typings/browser/环境/电子/electron.d.ts”。
  • 这是因为他们为服务器端和客户端提供了单独的定义文件 - 您可以在此处找到解决问题的说明:github.com/typings/typings#maindts-and-browserdts。我不确定你应该为 Electron 项目排除哪一个,所以你可能必须同时尝试。

标签: typescript electron visual-studio-code


【解决方案1】:

我修复了调试器无法识别电子模块的错误。问题是由于电子应用程序在我的应用程序启动之前没有启动。

我发现了一个解决此问题的 stackoverflow 问题和链接的博客文章 - Debugging Electron-Atom script with Visual Studio Code /
http://www.mylifeforthecode.com/a-better-way-to-launch-electron-from-visual-studio-code/

将"runtimeExecutable": "${workspaceRoot}/node_modules/electron-prebuilt/dist/electron" 行添加到我的“launch.json”文件中,在调试器启动之前启动电子。

我的最终“launch.json”文件是:

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Launch Black-Screen",
            "type": "node",
            "request": "launch",
            "program": "${workspaceRoot}/src/main/Main.ts",
            "stopOnEntry": false,
            "cwd": "${workspaceRoot}/src",
            "sourceMaps": true,
            "outDir": "${workspaceRoot}/src/bin",
            "runtimeExecutable": "${workspaceRoot}/node_modules/electron-prebuilt/dist/electron"
        }
    ]
}

调试器在我设置的断点处停止。我注意到使用调试器时电子的性能要慢得多,但这是我将解决的另一个问题:)

【讨论】:

  • 使用electron-prebuilt@0.37.6 我需要使用以下内容:"runtimeExecutable": "${workspaceRoot}/node_modules/electron-prebuilt/cli.js"
【解决方案2】:

项目包含https://github.com/shockone/black-screen/blob/master/typings/main/ambient/github-electron/github-electron.d.ts,模块声明为:https://github.com/shockone/black-screen/blob/master/typings/main/ambient/github-electron/github-electron.d.ts#L1884

怀疑错行:

"args": ["--project", "src", "--moduleResolution", "node"],

改为:

"args": ["-p", "./src"],

这在过去对我有用。

【讨论】:

  • 感谢 Basarat - 我在命令行“.vscode/tasks.json”文件和命令行中都尝试了新的打字稿参数。不幸的是,当我尝试在 vs 代码中启动应用程序时,我看到了与以前相同的错误。
猜你喜欢
  • 2018-07-26
  • 2019-05-25
  • 2017-05-25
  • 1970-01-01
  • 2019-05-09
  • 2022-06-15
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
相关资源
最近更新 更多