简体   繁体   English

ElectronJS:必须使用导入来加载 ES 模块

[英]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.

相关问题 节点错误必须使用import来加载ES模块 - Node Error Must use import to load ES module 错误:必须使用导入来加载 ES 模块 - 使用 Typescript 和 Node - Error: Must use import to load ES Module - Using Typescript and Node 必须使用import加载ES Module.eslintrc.js - Must use import to load ES Module .eslintrc.js Gulp 抛出错误“必须使用导入来加载 ES 模块” - Gulp throws error 'Must use import to load ES Module' 更新 angular 时必须使用 import 加载 ES Module - Must use import to load ES Module while updating angular ESLint - 错误:必须使用导入来加载 ES 模块 - ESLint - Error: Must use import to load ES Module webpack: “[ERR_REQUIRE_ESM]: Must use import to load ES Module” - 但我用的是 import! - webpack: “[ERR_REQUIRE_ESM]: Must use import to load ES Module” - But I AM using import! 错误 [ERR_REQUIRE_ESM]: 必须使用 import 来加载 ES 模块 - Error [ERR_REQUIRE_ESM]: Must use import to load ES module Vue-konva 运行出错:必须使用导入来加载 ES 模块 - Vue-konva running into error: Must use import to load ES Module NodeJS Typescrip.:出现错误:必须使用导入在 sls 离线命令上加载 ES 模块 - NodeJS Typescrip.: Getting error: Must use import to load ES Module on sls offline command
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM