[英]ElectronJS: Must use import to load ES Module
I've just started learning Electron from Electron Docs .我刚刚开始从Electron Docs学习 Electron 。 I used ES6
(import/export)
while they used require
.当他们使用
require
时,我使用了 ES6 (import/export)
。
I've added type:module
in my package.json
and also I've changed the js
extension to mjs
, but each time I run npm start
( My node version => 14 ), I get this error:我在我的
package.json
中添加了type:module
并且我已经将js
扩展名更改为mjs
,但是每次我运行npm start
(我的版本:> 145DC8FZ)
App threw an error during load
Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /Users/alaeddine/Desktop/electron-app/petro-book/main.mjs
at Module.load (internal/modules/cjs/loader.js:933:11)
at Module._load (internal/modules/cjs/loader.js:776:14)
at Function.f._load (electron/js2c/asar_bundle.js:5:12684)
at loadApplicationPackage (/Users/alaeddine/Desktop/electron-app/petro-book/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:110:16)
at Object.<anonymous> (/Users/alaeddine/Desktop/electron-app/petro-book/node_modules/electron/dist/Electron.app/Contents/Resources/default_app.asar/main.js:222:9)
at Module._compile (internal/modules/cjs/loader.js:1078:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:1108:10)
at Module.load (internal/modules/cjs/loader.js:935:32)
at Module._load (internal/modules/cjs/loader.js:776:14)
at Function.f._load (electron/js2c/asar_bundle.js:5:12684)
Package.json: Package.json:
{
"type": "module",
"name": "petro-book",
"version": "0.1.0",
"author": "Menai Ala Eddine",
"description": "Desktop application for gas stations",
"main": "main.mjs",
"scripts": {
"start": "electron main.mjs"
},
"devDependencies": {
"electron": "^12.0.7"
}
}
Main.mjs:主要.mjs:
import { app, BrowserWindow } from "electron";
import { join } from "path";
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: join(__dirname, "preload.js"),
},
});
win.loadFile("index.html");
}
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
preload.js preload.js
window.addEventListener("DOMContentLoaded", () => {
const replaceText = (selector, text) => {
const element = document.getElementById(selector);
if (element) element.innerText = text;
};
for (const type of ["chrome", "node", "electron"]) {
replaceText(`${type}-version`, process.versions[type]);
}
});
First you have to load esm in your node_modules.首先你必须在你的 node_modules 中加载 esm。
$ npm install esm --save $ npm 安装 esm --save
Put below config in your configuration.将以下配置放入您的配置中。 This is in my case:
这是我的情况:
"main": "src/index.cjs",
"module" : "src/main.mjs",
"type": "module",
Put Your main script to open window in main.mjs (using es6 style).将您的主脚本在 main.mjs 中打开window (使用 es6 样式)。
here is index.cjs as startup:这是index.cjs作为启动:
require = require("esm")(module/* , options */)
module.exports = require("./main.mjs")
and our main part in main.mjs以及我们在main.mjs中的主要部分
import electron from 'electron';
import path from 'path';
const app = electron.app;
const BrowserWindow = electron.BrowserWindow;
function createWindow() {
....
}
Check in here https://arahutara.wordpress.com/2022/07/13/using-es6-import-in-electron-apps/在这里签到 https://arahutara.wordpress.com/2022/07/13/using-es6-import-in-electron-apps/
Hopes this will help.希望这会有所帮助。
import
is used for ES6 Modules only in NodeJS Try using the following import
仅用于 NodeJS 中的 ES6 模块尝试使用以下命令
const { app, BrowserWindow } = require("electron");
const join = require("path").join;
function createWindow() {
const win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
preload: join(__dirname, "preload.js"),
},
});
win.loadFile("index.html");
}
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
The above is recommended and illustrated in the Electron Docs以上是Electron 文档中推荐和说明的
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.