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

Node.js服务器用brain.js遇gpu.js缺失及node-gyp编译错误求助

解决brain.js + gpu.js安装的node-gyp编译问题

问题背景

  • 使用brain.js构建神经网络,运行服务器时提示Cannot find module gpu.js
  • 执行npm i gpu.js安装后,出现node-gyp编译错误,已安装VS2022但问题未解决

错误日志

npm WARN deprecated uuid@3.4.0: Please upgrade  to version 7 or higher.  Older versions may use Math.random() in certain circumstances, which is known to be problematic.  See https://v8.dev/blog/math-random for details.
npm WARN deprecated request@2.88.2: request has been deprecated, see https://github.com/request/request/issues/3142
npm ERR! code 1
npm ERR! path C:\Users\priya\JSAIML\ytHelper\node_modules\gl
npm ERR! command failed
npm ERR! command C:\Windows\system32\cmd.exe /d /s /c prebuild-install || node-gyp rebuild
npm ERR! gyp info it worked if it ends with ok
npm ERR! gyp info using node-gyp@7.1.2
npm ERR! gyp info using node@18.8.0 | win32 | x64
npm ERR! gyp info find Python using Python version 3.10.3 found at "C:\Users\priya\AppData\Local\Programs\Python\Python310\python.exe"
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS msvs_version not set from command line or npm config
npm ERR! gyp ERR! find VS VCINSTALLDIR not set, not running in VS Command Prompt
npm ERR! gyp ERR! find VS could not use PowerShell to find Visual Studio 2017 or newer, try re-running with '--loglevel silly' for more details
npm ERR! gyp ERR! find VS looking for Visual Studio 2015
npm ERR! gyp ERR! find VS - not found
npm ERR! gyp ERR! find VS not looking for VS2013 as it is only supported up to Node.js 8
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS You need to install the latest version of Visual Studio
npm ERR! gyp ERR! find VS including the "Desktop development with C++" workload.
npm ERR! gyp ERR! find VS For more information consult the documentation at:
npm ERR! gyp ERR! find VS https://github.com/nodejs/node-gyp#on-windows
npm ERR! gyp ERR! find VS **************************************************************
npm ERR! gyp ERR! find VS
npm ERR! gyp ERR! configure error
npm ERR! gyp ERR! stack Error: Could not find any Visual Studio installation to use
npm ERR! gyp ERR! stack     at VisualStudioFinder.fail (C:\Users\priya\JSAIML\ytHelper\node_modules\node-gyp\lib\find-visualstudio.js:121:47)
npm ERR! gyp ERR! stack     at C:\Users\priya\JSAIML\ytHelper\node_modules\node-gyp\lib\find-visualstudio.js:74:16
npm ERR! gyp ERR! stack     at VisualStudioFinder.findVisualStudio2013 (C:\Users\priya\JSAIML\ytHelper\node_modules\node-gyp\lib\find-visualstudio.js:351:14)
npm ERR! gyp ERR! stack     at C:\Users\priya\JSAIML\ytHelper\node_modules\node-gyp\lib\find-visualstudio.js:70:14
npm ERR! gyp ERR! stack     at C:\Users\priya\JSAIML\ytHelper\node_modules\node-gyp\lib\find-visualstudio.js:372:16
npm ERR! gyp ERR! stack     at C:\Users\priya\JSAIML\ytHelper\node_modules\node-gyp\lib\util.js:54:7 
npm ERR! gyp ERR! stack     at C:\Users\priya\JSAIML\ytHelper\node_modules\node-gyp\lib\util.js:33:16npm ERR! gyp ERR! stack     at ChildProcess.exithandler (node:child_process:420:5)
npm ERR! gyp ERR! stack     at ChildProcess.emit (node:events:513:28)
npm ERR! gyp ERR! stack     at maybeClose (node:internal/child_process:1091:16)
npm ERR! gyp ERR! System Windows_NT 10.0.22000
npm ERR! gyp ERR! command "C:\\Users\\priya\\.nvm\\versions\\node\\v18.8.0\\bin\\node.exe" "C:\\Users\\priya\\JSAIML\\ytHelper\\node_modules\\node-gyp\\bin\\node-gyp.js" "rebuild"
npm ERR! gyp ERR! cwd C:\Users\priya\JSAIML\ytHelper\node_modules\gl
npm ERR! gyp ERR! node -v v18.8.0
npm ERR! gyp ERR! node-gyp -v v7.1.2
npm ERR! gyp ERR! not ok

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\priya\AppData\Local\npm-cache\_logs\2023-01-08T17_34_38_565Z-debug-0.log

项目代码

import express from 'express';
import * as brain from 'brain.js/dist/index.js';
import dotenv from 'dotenv'
import  ytData  from './md/data.js';


dotenv.config()
const app = express()
const PORT = process.env.PORT || 8000


const network = new brain.recurrent.LSTM();

const trainingData = ytData.map((item,i)=>({
    input : item.title,
    output : item.views,
}))


network.train(trainingData,{iterations : 2000});

const outputData = network.run("Simple Machine Learning With JavaScript - Brain.js")


app.get('/',(req,res)=>{
    // res.json({message : `On this title you will probably get around ${outputData} views`})
    res.send("hello")
})



app.listen(PORT, ()=>{
    console.log(`Running up the hill at ${PORT}km/hr`)
})

解决方案

1. 手动指定Visual Studio版本

node-gyp可能无法自动识别VS2022,执行以下命令配置:

npm config set msvs_version 2022

之后重新安装gpu.js:

npm i gpu.js --force

2. 使用VS2022开发者命令提示符安装

  • 打开开始菜单,搜索"Developer Command Prompt for VS 2022"并启动
  • 切换到项目根目录,执行npm i gpu.js

3. 跳过GPU依赖(快速方案)

如果不需要GPU加速,直接使用brain.js的纯CPU版本,无需安装gpu.js:
修改代码中的brain.js引入路径:

import * as brain from 'brain.js/dist/brain-browser.js';

此版本不依赖GPU,可直接运行,避免编译问题。

4. 检查node-gyp环境完整性

  • 确认VS2022已安装**Desktop development with C++**工作负载,包含MSVC工具链和对应版本的Windows SDK
  • 执行npm install -g node-gyp更新node-gyp到最新版本
  • 确保Python版本在3.6-3.10区间(当前环境3.10.3符合要求)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 06:50:29