如何使用 es6 import 导入 electron 而不是 require 函数
How to use es6 import for importing electron instead of require function
我是电子新手,使用节点 Js 制作 GUI。我参考了 electron 的文档,他们在那里使用 require 语句来导入模块。当我尝试使用 ES6 导入导入它时遇到错误。
npm version - 6.14.15
node version - v14.17.6
electron version - 16.0.7
package.json 文件看起来像这样 -
{
"name": "typing-speed-tester",
"version": "1.0.0",
"description": "This is a typing speed tester which will calculate your typing speed and accuracy of the typed words",
"main": "index.ts",
"scripts": {
"start": "electron ."
},
"author": "Gahan",
"license": "ISC",
"devDependencies": {
"electron": "^16.0.7"
}
}
这是 index.ts 文件
const { app, BrowserWindow } = require('electron');
const createWindow = () => {
const Window = new BrowserWindow({
width: 800, height: 600
})
Window.loadFile("index.html")
}
app.whenReady().then(() => {
createWindow();
})
这里我想把require换成import
关键字
在此先感谢您的帮助
您似乎缺少 Ts 编译器使用的模块分辨率设置。
您可以在项目根目录中添加一个 tsconfig.json
文件,其中包含一个 compilerOptions.module
值:
{
"compilerOptions": {
"module": "es2015"
}
}
有关详细信息,请参阅 Typescript documentation。
我是电子新手,使用节点 Js 制作 GUI。我参考了 electron 的文档,他们在那里使用 require 语句来导入模块。当我尝试使用 ES6 导入导入它时遇到错误。
npm version - 6.14.15
node version - v14.17.6
electron version - 16.0.7
package.json 文件看起来像这样 -
{
"name": "typing-speed-tester",
"version": "1.0.0",
"description": "This is a typing speed tester which will calculate your typing speed and accuracy of the typed words",
"main": "index.ts",
"scripts": {
"start": "electron ."
},
"author": "Gahan",
"license": "ISC",
"devDependencies": {
"electron": "^16.0.7"
}
}
这是 index.ts 文件
const { app, BrowserWindow } = require('electron');
const createWindow = () => {
const Window = new BrowserWindow({
width: 800, height: 600
})
Window.loadFile("index.html")
}
app.whenReady().then(() => {
createWindow();
})
这里我想把require换成import
关键字
在此先感谢您的帮助
您似乎缺少 Ts 编译器使用的模块分辨率设置。
您可以在项目根目录中添加一个 tsconfig.json
文件,其中包含一个 compilerOptions.module
值:
{
"compilerOptions": {
"module": "es2015"
}
}
有关详细信息,请参阅 Typescript documentation。