2016-02-29 68 views
3

当试图在Visual Studio代码(vs代码)中启动打印应用程序时,出现错误“无法找到模块电子”。我试图启动的项目是我从github克隆的​​。在Visual Studio中启动Typescript应用程序代码抛出错误“无法找到模块电子”

此错误是扔在了下面的语句:

import {ipcMain, nativeImage} from "electron";

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

我可以使用打字稿编译器(TSC)和transpile的申请会生成错误,并且可以在我期望的文件夹(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节点”中执行等效命令会生成没有错误或警告的经过转换的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 
|... 

任何帮助,将不胜感激:)

+1

工作的你有一个Electron的定义文件('.d.ts')吗?如果没有这个,Typescript语言服务将无法辨别它是否存在。 –

+1

事实上,我刚刚注意到,你克隆的repo有一个' typings.json' - 尝试运行'npm install typings -g'然后'typings install'到项目目录 –

+0

谢谢Joe。当我看到Basarat的回答后,我检查并看到“typings”目录已经存在于存储库文件中。确实尝试安装类型('npm我nstall -g typings'&'typings install')。 – Michael

回答

3

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

我发现了一个问题,计算器和链接的博客张贴这解决了这个问题 - 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”文件电子启动调试器之前推出。

我的最终“推出。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" 
     } 
    ] 
} 

调试器是在我设置的断点停止我注意到电子的性能是使用调试器要慢得多,但是那是另外一个问题,我会通过:)

+2

使用'electron-prebuilt @ 0.37.6'我需要使用以下内容:''runtimeExecutable“:”$ {workspaceRoot} /node_modules/electron-prebuilt/cli.js“' – Manu

1

该项目包含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"], 

因为这在过去对我有效。

+0

感谢Basarat - 我在命令行'.vscode/tasks.json'文件和命令行中尝试了新的typescript参数。不幸的是,当我尝试在vs代码中启动应用程序时,我看到了和以前一样的错误。 – Michael