typescript - Electron 和 typescript "Cannot find module ' Electron '"

标签 typescript electron

关于 https://electron.atom.io/blog/2017/06/01/typescript Electron 支持 typescript 但不适用于我的设置:

[ts] cannot find module 'electron'

我用的是vscode 1.16.1

这是我的 package.json

{
  [...]
  "devDependencies": {
    "electron": "^1.6.13",
    "ts-loader": "~2.3.7",
    "typescript": "~2.5.0",
    "webpack": "^3.6.0",
    [...]
  }
}

tsconfig.json

{
    "compilerOptions": {
        "module": "es6",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true
    },
    "include": [
        "src/**/*"
    ]
}

和我的网络包

const path = require('path');

module.exports = [{
  entry: './src/main.ts',
  devtool: 'inline-source-map',
  target: 'electron',
  module: {
    rules: [
      { test: /\.ts$/, use: 'ts-loader', exclude: /node_modules/ }
    ]
  },
  node: {
    __dirname: false,
    __filename: false
  },
  resolve: {
    extensions: [".ts", ".js"]
  },
  output: {
    filename: 'electron_core.js',
    path: path.resolve(__dirname, 'dist')
  }
}  
];

当我在 main.ts 的顶部添加时

///<reference path="../node_modules/electron/electron.d.ts" />

然后就可以了,我不再有错误了。但是我想避免引用这样的文件,因为它似乎对最新版本的 typescript 毫无用处(请参阅 How do I import other TypeScript files? )而且在 typescript 的 Electron 教程中他们不需要它......)

谢谢

最佳答案

问题似乎在于 tsc(和 tsserver)解析模块的方式 by default .

要使用类似 node.js 的算法,您需要将 "moduleResolution": "node" 添加到 tsconfig.json< 的 "compilerOptions" 部分.

关于typescript - Electron 和 typescript "Cannot find module ' Electron '",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46562367/

相关文章:

typescript - Angular 5/Material,如何删除 Mat Dialog 中的垂直滚动?

javascript - 如何从其他本地文件Node.js调用函数?

node.js - Electron - 如何生成文件校验和

javascript - 如何在 electron shell 中做 oauth 2.0

angular - 检测 Angular 形式(非响应式)中的数据是否已更改

function - 展开对象以作为函数参数传递

typescript - 在 TS 中导出类似 "module.exports"

Typescript 接口(interface),其中 "id"必须是数字,其他属性必须是字符串

node.js - Atom Electron 不适用于 Raspberry Pi

electron - 在 Electron 应用程序的安装过程中注册自定义协议(protocol)