关于 https://electron.atom.io/blog/2017/06/01/typescript Electron 支持 typescript 但不适用于我的设置:
我用的是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/