【发布时间】: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") 成功启动应用程序。
以下是相关的项目配置文件:
-
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" } } -
.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" } -
.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 <variable-name>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