Electron 应用程序的“找不到模块错误”

问题描述 投票:0回答:1

我正在电子中制作音频增强器应用程序,但是当我单击增强按钮时,在控制台上出现此错误

(node:7587) UnhandledPromiseRejectionWarning: Error [ERR_MODULE_NOT_FOUND]: Cannot find module '/home/adi/spectrum/node_modules/tone/build/esm/core/Global' imported from /home/adi/spectrum/node_modules/tone/build/esm/index.js
    at finalizeResolution (node:internal/modules/esm/resolve:264:11)
    at moduleResolve (node:internal/modules/esm/resolve:924:10)
    at defaultResolve (node:internal/modules/esm/resolve:1137:11)
    at ModuleLoader.defaultResolve (node:internal/modules/esm/loader:396:12)
    at ModuleLoader.resolve (node:internal/modules/esm/loader:365:25)
    at ModuleLoader.getModuleJob (node:internal/modules/esm/loader:240:38)
    at ModuleWrap.<anonymous> (node:internal/modules/esm/module_job:85:39)
    at link (node:internal/modules/esm/module_job:84:36)
(Use `electron --trace-warnings ...` to show where the warning was created)
(node:7587) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 8)

,主要js:

const { app, BrowserWindow, ipcMain } = require('electron')
const path = require('path');
function createWindow () {
  const win = new BrowserWindow({
    width: 800,
    height: 600,
    webPreferences: {
      nodeIntegration: true,
      contextIsolation: false, // Consider revisiting for security in future development
      preload: path.join(__dirname, 'preload.js') // Optional preload script for additional security (refer to Electron documentation)
    }
  })

  win.loadFile('index.html')
}

app.whenReady().then(createWindow)

app.on('window-all-closed', () => {
  if (process.platform !== 'darwin') {
    app.quit()
  }
})

try {
  ipcMain.on('enhance-audio', async (event, filePath) => {
  
    const Tone = await import('tone')
  
    const player = new Tone.Player(filePath).toDestination()
    const eq = new Tone.EQ({
      low: 0, 
      mid: 1,
      high: 0
    }).toDestination()
    player.connect(eq)
    player.start()
  
    const outputPath = 'enhanced_audio.mp3' // Replace with your logic
    event.sender.send('enhancement-complete', outputPath)
  })
  
} catch (error) {
  console.log("error",error)
}

这是一个主要的js文件,它负责大部分工作。

下面是renderer.js

const ipcRenderer = require('electron').ipcRenderer

const audioUpload = document.getElementById('audio-upload')
const enhanceButton = document.getElementById('enhance-button')
const outputFilename = document.getElementById('output-filename')

enhanceButton.addEventListener('click', () => {
  const filePath = audioUpload.files[0].path
  console.log("Sending file path:", filePath);
  ipcRenderer.send('enhance-audio', filePath)
})

ipcRenderer.on('enhancement-complete', (event, outputPath) => {
  outputFilename.textContent = `Enhanced file: ${outputPath}`
})

这是索引 html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Spectrum</title>
</head>
<body>
  <h1>Audio Enhancer</h1>
  <input type="file" id="audio-upload">
  <button id="enhance-button">Enhance</button>
  <p id="output-filename"></p>

  <script src="renderer.js"></script>
</body>
</html>

和 这是我的包 json

{
  "name": "spectrum",
  "version": "1.0.0",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "electron": "^30.0.3"
  },
  "dependencies": {
    "path": "^0.12.7",
    "tone": "^14.9.17"
  }
}

我尝试了 Gemini 和 CHATGPT 并尝试重新导入库,但它工作正常

javascript node.js electron
1个回答
0
投票

Tone.js问题 的快速研究表明,很多人都遇到了与您相同的错误(例如,请参阅问题 #973#1077)。您收到此错误的原因是因为 Tone.js 是一个 ESM 包。这意味着除非您的 Electron 应用程序也是 ESM,否则您无法使用它。您有两种解决方案:

1.使您的应用程序 ESM

为此,您可以按照官方文档进行操作。正确的导入将是:

import * as Tone from "tone";

2.降级软件包版本

您可以将 Tone.js 的版本降级到成为 ESM 包之前。根据问题(因为似乎包文档和版本都缺少此信息),它将是版本

14.7.0

不使用ESM(或捆绑器),也意味着你不能使用ES6

import
syntaxx,你必须使用CJS
require
:

const Tone = require("tone");
© www.soinside.com 2019 - 2024. All rights reserved.