2016-02-29 43 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

相關問題