Windows环境下安装最新版@tensorflow/tfjs-node失败求助
Windows下安装@tensorflow/tfjs-node最新版本失败问题
环境与现象
- Node.js版本:v17.9.1
- npm版本:v9.1.2
- 可正常安装
@tensorflow/tfjs,但无法安装最新版@tensorflow/tfjs-node,仅能成功安装1.2.8版本 - 已尝试操作:安装node-gyp、windows-build-tools@4.0.0,删除node_modules、清理npm缓存、移除package.json后重新安装
安装报错信息
npm ERR! code 1 npm ERR! path D:\WinUsers\User\Desktop\img\node_modules\@tensorflow\tfjs-node npm ERR! command failed npm ERR! command C:\WINDOWS\system32\cmd.exe /d /s /c node scripts/install.js npm ERR! CPU-windows-4.6.0.zip npm ERR! https://storage.googleapis.com/tensorflow/libtensorflow/libtensorflow-cpu-windows-x86_64-2.9.1.zip npm ERR! node-pre-gyp install failed with error: Error: Command failed: node-pre-gyp install --fallback-to-build npm ERR! node-pre-gyp info it worked if it ends with ok npm ERR! node-pre-gyp info using node-pre-gyp@1.0.9 npm ERR! node-pre-gyp info using node@17.9.1 | win32 | x64 npm ERR! node-pre-gyp info check checked for "D:\WinUsers\User\Desktop\img\node_modules\@tensorflow\tfjs-node\lib\napi-v8\tfjs_binding.node" (not found) npm ERR! node-pre-gyp http GET https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/4.6.0/CPU-windows-4.6.0.zip npm ERR! node-pre-gyp ERR! install response status 404 Not Found on https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/4.6.0/CPU-windows-4.6.0.zip npm ERR! node-pre-gyp WARN Pre-built binaries not installable for @tensorflow/tfjs-node@4.6.0 and node@17.9.1 (node-v102 ABI, unknown) (falling back to source compile with node-gyp) npm ERR! node-pre-gyp WARN Hit error response status 404 Not Found on https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/4.6.0/CPU-windows-4.6.0.zip npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@9.3.1 npm ERR! gyp info using node@17.9.1 | win32 | x64 npm ERR! gyp info ok npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@9.3.1 npm ERR! gyp info using node@17.9.1 | win32 | x64 npm ERR! gyp info find Python using Python version 3.11.1 found at "C:\Users\User\AppData\Local\Programs\Python\Python311\python.exe" npm ERR! gyp info find VS using VS2022 (17.3.32901.215) found at: npm ERR! gyp info find VS "E:\vsc" npm ERR! gyp info find VS run with --verbose for detailed information npm ERR! gyp info spawn C:\Users\User\AppData\Local\Programs\Python\Python311\python.exe npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args 'C:\\Users\\User\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\gyp\\gyp_main.py', npm ERR! gyp info spawn args 'binding.gyp', npm ERR! gyp info spawn args '-f', npm ERR! gyp info spawn args 'msvs', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args 'D:\\WinUsers\\User\\Desktop\\img\\node_modules\\@tensorflow\\tfjs-node\\build\\config.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args 'C:\\Users\\User\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\addon.gypi', npm ERR! gyp info spawn args '-I', npm ERR! gyp info spawn args 'C:\\Users\\User\\AppData\\Local\\node-gyp\\Cache\\17.9.1\\include\\node\\common.gypi', npm ERR! gyp info spawn args '-Dlibrary=shared_library', npm ERR! gyp info spawn args '-Dvisibility=default', npm ERR! gyp info spawn args '-Dnode_root_dir=C:\\Users\\User\\AppData\\Local\\node-gyp\\Cache\\17.9.1', npm ERR! gyp info spawn args '-Dnode_gyp_dir=C:\\Users\\User\\AppData\\Roaming\\npm\\node_modules\\node-gyp', npm ERR! gyp info spawn args '-Dnode_lib_file=C:\\\\Users\\\\User\\\\AppData\\\\Local\\\\node-gyp\\\\Cache\\\\17.9.1\\\\<(target_arch)\\\\node.lib', npm ERR! gyp info spawn args '-Dmodule_root_dir=D:\\WinUsers\\User\\Desktop\\img\\node_modules\\@tensorflow\\tfjs-node', npm ERR! gyp info spawn args '-Dnode_engine=v8', npm ERR! gyp info spawn args '--depth=.', npm ERR! gyp info spawn args '--no-parallel', npm ERR! gyp info spawn args '--generator-output', npm ERR! gyp info spawn args 'D:\\WinUsers\\User\\Desktop\\img\\node_modules\\@tensorflow\\tfjs-node\\build', npm ERR! gyp info spawn args '-Goutput_dir=.' npm ERR! gyp info spawn args ] npm ERR! gyp info ok npm ERR! gyp info it worked if it ends with ok npm ERR! gyp info using node-gyp@9.3.1 npm ERR! gyp info using node@17.9.1 | win32 | x64 npm ERR! gyp info spawn E:\vsc\MSBuild\Current\Bin\MSBuild.exe npm ERR! gyp info spawn args [ npm ERR! gyp info spawn args 'build/binding.sln', npm ERR! gyp info spawn args '/clp:Verbosity=minimal', npm ERR! gyp info spawn args '/nologo', npm ERR! gyp info spawn args '/p:Configuration=Release;Platform=x64' npm ERR! gyp info spawn args ] npm ERR! gyp ERR! build error npm ERR! gyp ERR! stack Error: `E:\vsc\MSBuild\Current\Bin\MSBuild.exe` failed with exit code: 1 npm ERR! gyp ERR! stack at ChildProcess.onExit (C:\Users\User\AppData\Roaming\npm\node_modules\node-gyp\lib\build.js:203:23) npm ERR! gyp ERR! stack at ChildProcess.emit (node:events:527:28) npm ERR! gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:291:12) npm ERR! gyp ERR! System Windows_NT 10.0.18363 npm ERR! gyp ERR! command "E:\\nodejs\\node.exe" "C:\\Users\\User\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "build" "--fallback-to-build" "--module=D:\\WinUsers\\User\\Desktop\\img\\node_modules\\@tensorflow\\tfjs-node\\lib\\napi-v8\\tfjs_binding.node" "--module_name=tfjs_binding" "--module_path=D:\\WinUsers\\User\\Desktop\\img\\node_modules\\@tensorflow\\tfjs-node\\lib\\napi-v8" "--napi_version=8" "--node_abi_napi=napi" "--napi_build_version=8" "--node_napi_label=napi-v8" npm ERR! gyp ERR! cwd D:\WinUsers\User\Desktop\img\node_modules\@tensorflow\tfjs-node npm ERR! gyp ERR! node -v v17.9.1 npm ERR! gyp ERR! node-gyp -v v9.3.1 npm ERR! gyp ERR! not ok npm ERR! node-pre-gyp ERR! build error npm ERR! node-pre-gyp ERR! stack Error: Failed to execute 'E:\nodejs\node.exe C:\Users\User\AppData\Roaming\npm\node_modules\node-gyp\bin\node-gyp.js build --fallback-to-build --module=D:\WinUsers\User\Desktop\img\node_modules\@tensorflow\tfjs-node\lib\napi-v8\tfjs_binding.node --module_name=tfjs_binding --module_path=D:\WinUsers\User\Desktop\img\node_modules\@tensorflow\tfjs-node\lib\napi-v8 --napi_version=8 --node_abi_napi=napi --napi_build_version=8 --node_napi_label=napi-v8' (1) npm ERR! node-pre-gyp ERR! stack at ChildProcess.<anonymous> (D:\WinUsers\User\Desktop\img\node_modules\@mapbox\node-pre-gyp\lib\util\compile.js:89:23) npm ERR! node-pre-gyp ERR! stack at ChildProcess.emit (node:events:527:28) npm ERR! node-pre-gyp ERR! stack at maybeClose (node:internal/child_process:1090:16) npm ERR! node-pre-gyp ERR! stack at Process.ChildProcess._handle.onexit (node:internal/child_process:302:5) npm ERR! node-pre-gyp ERR! System Windows_NT 10.0.18363 npm ERR! node-pre-gyp ERR! command "E:\\nodejs\\node.exe" "D:\\WinUsers\\User\\Desktop\\img\\node_modules\\@mapbox\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build" npm ERR! node-pre-gyp ERR! cwd D:\WinUsers\User\Desktop\img\node_modules\@tensorflow\tfjs-node npm ERR! node-pre-gyp ERR! node -v v17.9.1 npm ERR! node-pre-gyp ERR! node-pre-gyp -v v1.0.9 npm ERR! node-pre-gyp ERR! not ok npm ERR! * Downloading libtensorflow npm ERR! npm ERR! * Building TensorFlow Node.js bindings
解决方案分析与步骤
核心错误原因
- 预编译二进制包缺失:报错显示
404 Not Found,说明@tensorflow/tfjs-node@4.6.0没有为Node.js v17.9.1(对应ABI版本node-v102)提供Windows平台的预编译二进制包,只能回退到源码编译。 - 源码编译失败:MSBuild编译出错,原因可能是Node.js版本与tfjs-node版本不兼容,或者Python、VS版本不匹配。
具体解决步骤
步骤1:降级Node.js到兼容版本
Node.js v17是奇数版本(非LTS),tfjs-node对LTS版本的支持更完善。建议降级到Node.js v16.x(LTS)或v18.x(LTS):
- 使用nvm管理Node.js版本(推荐):
# 安装nvm后执行 nvm install 16.20.2 nvm use 16.20.2 - 或直接从Node.js官网下载对应版本安装包覆盖现有版本。
步骤2:重新安装tfjs-node
切换Node.js版本后,清理缓存并重新安装:
npm cache clean --force rm -rf node_modules package-lock.json npm install @tensorflow/tfjs-node@latest
步骤3:调整编译依赖(若需保留Node.js v17)
如果必须使用Node.js v17,调整编译环境:
- 降级Python版本:tfjs-node源码编译对Python 3.11支持不佳,建议安装Python 3.9.x或3.10.x,并通过以下命令指定路径:
npm config set python "C:\你的Python安装路径\python.exe" - 确认VS组件:确保VS2022安装了「桌面开发使用C++」组件,包含MSVC工具链、Windows SDK等。
- 手动指定msbuild路径:若msbuild无法被识别,设置环境变量:
npm config set msbuild_path "E:\vsc\MSBuild\Current\Bin\MSBuild.exe"
步骤4:尝试安装tfjs-node-gpu(可选)
如果有NVIDIA显卡,可尝试安装GPU版本,兼容性可能更好:
npm install @tensorflow/tfjs-node-gpu@latest
内容的提问来源于stack exchange,提问作者tysik
相关产品推荐
相关产品推荐

