You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Electron音频增强应用触发ERR_MODULE_NOT_FOUND错误求助

音频增强Electron应用模块找不到错误排查与解决

错误日志

点击“增强”按钮时控制台输出如下错误:

(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)

项目代码文件

main.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)
}

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}`
})

index.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>

package.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"
  }
}

解决方案

1. 修复Tone.js模块导入方式

Electron主进程默认使用CommonJS模块系统,动态导入ESM模块易出现路径解析错误,改为CommonJS导入:

// 替换 main.js 中的 const Tone = await import('tone')
const Tone = require('tone')

2. 捕获异步回调中的错误

原代码的try-catch仅覆盖事件注册逻辑,未处理回调内的异步错误,修改ipcMain回调:

ipcMain.on('enhance-audio', async (event, filePath) => {
  try {
    const Tone = require('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'
    event.sender.send('enhancement-complete', outputPath)
  } catch (error) {
    console.error('音频增强失败:', error)
    event.sender.send('enhancement-error', error.message)
  }
})

3. 重新安装依赖确保完整性

删除现有依赖文件后重新安装,避免Tone.js安装不完整导致文件缺失:

rm -rf node_modules package-lock.json
npm install

4. 移除多余依赖

path是Node.js内置模块,无需在package.json中声明,删除该依赖项:

// 修改后的 package.json dependencies 字段
"dependencies": {
  "tone": "^14.9.17"
}

5. 可选:添加前端错误反馈

在renderer.js中新增错误监听,给用户显示直观提示:

ipcRenderer.on('enhancement-error', (event, errorMsg) => {
  outputFilename.textContent = `增强失败: ${errorMsg}`
})

内容的提问来源于stack exchange,提问作者ADITYA

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 04:59:52