import fetch from "node-fetch";
const electron = require("electron");
const url = require("url");
const path = require("path");
const { app, BrowserWindow, Menu, ipcMain } = electron;
let mainWindow;
let addWindow;
function createAddWindow() {
addWindow = new BrowserWindow({
width: 300,
height: 200,
title: "Add Shopping List Item",
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
preload: path.join(__dirname, "addWindow.js"),
},
});
addWindow.loadURL(
url.format({
pathname: path.join(__dirname, "addWindow.html"),
protocol: "file:",
slashes: true,
})
);
// Handle garbage collection
addWindow.on("close", function () {
addWindow = null;
});
}
const mainMenuTemplate = [
{
label: "File",
submenu: [
{
label: "Quit",
accelerator: process.platform == "darwin" ? "Command+Q" : "Ctrl+Q",
click() {
app.quit();
},
},
{
label: "Add Food",
accelerator: process.platform == "darwin" ? "Command+N" : "Ctrl+N",
click() {
createAddWindow();
console.log(__dirname);
},
},
],
},
];
if (process.env.NODE_ENV !== "production") {
mainMenuTemplate.push({
label: "Developer Tools",
submenu: [
{
role: "reload",
},
{
label: "Toggle DevTools",
accelerator: process.platform == "darwin" ? "Command+I" : "Ctrl+I",
click(item, focusedWindow) {
focusedWindow.toggleDevTools();
},
},
],
});
}
const mainMenu = Menu.buildFromTemplate(mainMenuTemplate);
Menu.setApplicationMenu(mainMenu);
app.on("ready", function () {
mainWindow = new BrowserWindow({
width: 800,
webPreferences: {
nodeIntegration: false, // is default value after Electron v5
contextIsolation: true, // protect against prototype pollution
enableRemoteModule: false, // turn off remote
preload: path.join(__dirname, "addWindow.js"), // use a preload script
},
});
mainWindow.loadURL(
url.format({
pathname: path.join(__dirname, "mainWindow.html"),
protocol: "file:",
slashes: true,
})
);
});
global.foods = [];
ipcMain.on("item:add", (e, item) => {
if (global.foods) {
global.foods.push(item);
}
let params = {
api_key: "hufCDnLVnrKMT18TsIPBJYVDnzwyQaLyKfLE9Q6c",
query: item,
dataType: [" Survey (FNDDS)"],
pagesize: 5,
};
let api_url = `https://api.nal.usda.gov/fdc/v/foods/search?api_key={encodeURIComponent(params.api_key)}&query=${encodeURIComponent(
params.query
)}&dataType=${encodeURIComponent(
params.dataType
)}&pageSize=${encodeURIComponent(params.pagesize)}`;
function getData() {
return fetch(api_url).then((response) => response.json());
}
getData().then((data) => console.log(data.foods[0]).foodNutrients);
addWindow.close();
});
Package.json
{
"name": "count-it-lose-it",
"version": "1.0.0",
"description": "Lose weight , workout , calorie counter",
"main": "main.js",
"scripts": {
"start": "electron ."
},
"author": "Keegan Albert",
"license": "MIT",
"dependencies": {
"axios": "^0.27.2",
"electron": "^20.0.2",
"node-fetch": "^3.2.10"
},
"type": "module"
}
App threw an error during load Error [ERR_REQUIRE_ESM]: require() of ES Module F:\workout app\main.js from F:\workout app\node_modules\electron\dist\resources\default_app.asar\main.js not supported. Instead change the require of F:\workout app\main.js in F:\workout app\node_modules\electron\dist\resources\default_app.asar\main.js to a dynamic import() which is available in all CommonJS modules. at c._load (node:electron/js2c/asar_bundle:5:13343) at loadApplicationPackage (F:\workout app\node_modules\electron\dist\resources\default_app.asar\main.js:110:16) at Object. (F:\workout app\node_modules\electron\dist\resources\default_app.asar\main.js:222:9) at c._load (node:electron/js2c/asar_bundle:5:13343) at Object. (node:electron/js2c/browser_init:185:3104) at ./lib/browser/init.ts (node:electron/js2c/browser_init:185:3308) at webpack_require (node:electron/js2c/browser_init:1:128) at node:electron/js2c/browser_init:1:1200 at node:electron/js2c/browser_init:1:1267 at c._load (node:electron/js2c/asar_bundle:5:13343)
this is the error
1条答案
按热度按时间a9wyjsp71#
您是否尝试过要求获取而不是导入它?
const fetch = require("node-fetch")
并从package.json中删除
type: "module"
编辑:您应该将
electron
作为一个dev依赖项安装,因为稍后很可能需要将其打包用于生产。