电子和打字稿"找不到模块'电子'"

Ada*_*avo 11 typescript electron

关于https://electron.atom.io/blog/2017/06/01/typescript 电子支持打字稿但是我的设置无效:

[ts]无法找到模块'电子'

我使用vscode 1.16.1

这是我的package.json

{
  [...]
  "devDependencies": {
    "electron": "^1.6.13",
    "ts-loader": "~2.3.7",
    "typescript": "~2.5.0",
    "webpack": "^3.6.0",
    [...]
  }
}
Run Code Online (Sandbox Code Playgroud)

tsconfig.json

{
    "compilerOptions": {
        "module": "es6",
        "noImplicitAny": true,
        "removeComments": true,
        "preserveConstEnums": true,
        "sourceMap": true
    },
    "include": [
        "src/**/*"
    ]
}
Run Code Online (Sandbox Code Playgroud)

和我的网络包

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')
  }
}  
];
Run Code Online (Sandbox Code Playgroud)

当我在main.ts的顶部添加

///<reference path="../node_modules/electron/electron.d.ts" />
Run Code Online (Sandbox Code Playgroud)

那么没关系我没有错误了.但是我想避免引用这样的文件,因为它似乎对最新版本的打字稿没用(请参阅如何导入其他TypeScript文件?)而且在电子教程中对于打字稿,他们不需要它......)

谢谢

Kir*_*nko 18

问题似乎在于tsc(和tsserver)默认情况下恢复模块的方式.

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

  • 嗨,我面临着同样的问题。从“电子”导入{shell};//给出“找不到模块'电子'” 我还在 tsconfig.json 文件中进行了上面建议的更改。如果有人对此错误有正确的解决方案,请提供帮助。 (2认同)