Skip to content

require('@tensorflow/tfjs-node') error #8545

Closed
@gggboy-9527

Description

@gggboy-9527

test code: const tf = require('@tensorflow/tfjs-node'); console.log('123')

error:C:\workspace\handleremb> node test.js node:internal/modules/cjs/loader:1460 return process.dlopen(module, path.toNamespacedPath(filename)); ^ Error: The specified module could not be found. \\?\C:\workspace\handleremb\node_modules\@tensorflow\tfjs-node\lib\napi-v8\tfjs_binding.node at Module._extensions..node (node:internal/modules/cjs/loader:1460:18) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) at Module.require (node:internal/modules/cjs/loader:1231:19) at require (node:internal/modules/helpers:177:18) at Object.<anonymous> (C:\workspace\handleremb\node_modules\@tensorflow\tfjs-node\dist\index.js:72:16) at Module._compile (node:internal/modules/cjs/loader:1364:14) at Module._extensions..js (node:internal/modules/cjs/loader:1422:10) at Module.load (node:internal/modules/cjs/loader:1203:32) at Module._load (node:internal/modules/cjs/loader:1019:12) { code: 'ERR_DLOPEN_FAILED'

then i input:node-gyp configure --verbose

then C:\workspace\handleremb> node-gyp configure --verbose ... 'C:\\workspace\\handleremb\\build', gyp info spawn args '-Goutput_dir=.' gyp info spawn args ] gyp: binding.gyp not found (cwd: C:\workspace\handleremb) while trying to load binding.gyp gyp ERR! configure error gyp ERR! stack Error: gyp failed with exit code: 1 gyp ERR! stack at ChildProcess.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configure.js:317:18) gyp ERR! stack at ChildProcess.emit (node:events:517:28) gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:292:12) gyp ERR! System Windows_NT 10.0.20348 gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "configure" "--verbose" gyp ERR! cwd C:\workspace\handleremb gyp ERR! node -v v18.20.4 gyp ERR! node-gyp -v v11.2.0 gyp ERR! not ok

He said that the file could not be found in binding.gyp, so I went to the directory of node_madules and found the location of the file. Then I executed the previous command as follows (only the error part is pasted here):
gyp: Undefined variable module_name in binding.gyp while trying to load binding.gyp gyp ERR! configure error gyp ERR! configure error gyp ERR! stack Error: gyp failed with exit code: 1 gyp ERR! stack at ChildProcess.<anonymous> (C:\Users\Administrator\AppData\Roaming\npm\node_modules\node-gyp\lib\configure.js:317:18) gyp ERR! stack at ChildProcess.emit (node:events:517:28) gyp ERR! stack at ChildProcess._handle.onexit (node:internal/child_process:292:12) gyp ERR! System Windows_NT 10.0.20348 gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Administrator\\AppData\\Roaming\\npm\\node_modules\\node-gyp\\bin\\node-gyp.js" "configure" "--verbose" gyp ERR! cwd C:\workspace\handleremb\node_modules\@tensorflow\tfjs-node gyp ERR! node -v v18.20.4 gyp ERR! node-gyp -v v11.2.0 gyp ERR! not ok ;

then I asked Deepseek, and his explanation was to modify the binding.gyp file. This is the modified file
{ "variables": { "tensorflow_include_dir": "<(module_root_dir)/deps/include", "tensorflow_headers": [ "<@(tensorflow_include_dir)/tensorflow/c/c_api.h", "<@(tensorflow_include_dir)/tensorflow/c/eager/c_api.h" ], "tensorflow-library-action": "move", _**"module_name": "tfjs_binding"**_ }, "targets": [ { "target_name": "tfjs_binding", "sources": [ "binding/tfjs_backend.cc", "binding/tfjs_binding.cc" ], "include_dirs": [ "..", "<(tensorflow_include_dir)" ], "conditions": [ [ "OS=='linux'", { "libraries": [ "-Wl,-rpath,\\$$ORIGIN/../../deps/lib", "-ltensorflow", "-ltensorflow_framework" ], "library_dirs": [ "<(module_root_dir)/deps/lib" ] }], [ "OS=='mac'", { "libraries": [ "<(module_root_dir)/deps/lib/libtensorflow.2.dylib", "<(module_root_dir)/deps/lib/libtensorflow_framework.2.dylib" ], "postbuilds": [ { "postbuild_name": "Adjust libtensorflow load path", "action": [ "install_name_tool", "-change", "@rpath/libtensorflow.2.dylib", "@loader_path/../../deps/lib/libtensorflow.2.dylib", "<(PRODUCT_DIR)/tfjs_binding.node" ] }, { "postbuild_name": "Adjust libtensorflow_framework load path", "action": [ "install_name_tool", "-change", "@rpath/libtensorflow_framework.2.dylib", "@loader_path/../../deps/lib/libtensorflow_framework.2.dylib", "<(PRODUCT_DIR)/tfjs_binding.node" ] } ] }], [ "OS=='win'", { "defines": [ "COMPILER_MSVC" ], _**"libraries": [ "tensorflow.lib" ]**_, "library_dirs": [ "<(module_root_dir)/deps/lib" ], "variables": { "tensorflow-library-target": "windows" }, "msvs_disabled_warnings": [ 4190 ] }] ] }, { "target_name": "action_after_build", "type": "none", _**"dependencies": [ "tfjs_binding" ],**_ "copies": [ { "files": [ "<(PRODUCT_DIR)/tfjs_binding.node" ], "destination": "<(module_path)" } ] } ], "defines": [ "NAPI_VERSION=<(napi_build_version)" ] }
then Then I rebuilt it and executed it again, but it encountered this error:
Undefined variable module_path in binding.gyp while trying to load binding.gyp gyp ERR! configure error gyp ERR! stack Error: gyp failed with exit code: 1

then i modify :
{ "variables": { "tensorflow_include_dir": "<(module_root_dir)/deps/include", "tensorflow_headers": [ "<@(tensorflow_include_dir)/tensorflow/c/c_api.h", "<@(tensorflow_include_dir)/tensorflow/c/eager/c_api.h" ], "tensorflow-library-action": "move", "module_name": "tfjs_binding", "module_path": "<(module_root_dir)/lib/napi-v8" }, "targets": [ # ... ], "defines": [ "NAPI_VERSION=<(napi_build_version)" ] }

Then I performed code testing, which should have been successful
`node test.js 2025-04-14 14:48:46.192271: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
123

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions