错误:在路径中找不到Node.js本机附加模块(tfjs_binding.node)

时间:2020-06-18 04:32:46

标签: javascript node.js tensorflow npm

我尝试运行this应用的示例:

enter image description here

通过此脚本:

const tf = require('@tensorflow/tfjs-node');
const posenet = require('@tensorflow-models/posenet');
const {
    createCanvas, Image
} = require('canvas')
const imageScaleFactor = 0.5;
const outputStride = 16;
const flipHorizontal = false;

const tryModel = async() => {
    console.log('start');
    const net = await posenet.load(0.75);
    const img = new Image();
    img.src = './test.jpg';
    const canvas = createCanvas(img.width, img.height);
    const ctx = canvas.getContext('2d');
    ctx.drawImage(img, 0, 0);
    const input = tf.browser.fromPixels(canvas);
    const pose = await net.estimateSinglePose(input, imageScaleFactor, flipHorizontal, outputStride);
    // console.log(pose);
    for(const keypoint of pose.keypoints) {
        console.log(`${keypoint.part}: (${keypoint.position.x},${keypoint.position.y})`);
    }
    console.log('end');
}



tryModel();

但是当我尝试时:

node index.js

我收到此错误:

so@so-notebook:~/Desktop/trash/tensorflow$ node index.js
Overriding the gradient for 'Max'
Overriding the gradient for 'OneHot'
Overriding the gradient for 'PadV2'
Overriding the gradient for 'SpaceToBatchND'
Overriding the gradient for 'SplitV'
/home/so/node_modules/@tensorflow/tfjs-node/dist/index.js:49
    throw new Error("The Node.js native addon module (tfjs_binding.node) can not "
    ^

Error: The Node.js native addon module (tfjs_binding.node) can not be found at path: /home/so/node_modules/@tensorflow/tfjs-node/lib/napi-v6/tfjs_binding.node. 
Please run command 'npm rebuild @tensorflow/tfjs-node build-addon-from-source' to rebuild the native addon module. 
If you have problem with building the addon module, please check https://github.com/tensorflow/tfjs/blob/master/tfjs-node/WINDOWS_TROUBLESHOOTING.md or file an issue.
    at Object.<anonymous> (/home/so/node_modules/@tensorflow/tfjs-node/dist/index.js:49:11)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)
    at Function.Module._load (internal/modules/cjs/loader.js:879:14)
    at Module.require (internal/modules/cjs/loader.js:1026:19)
    at require (internal/modules/cjs/helpers.js:72:18)
    at Object.<anonymous> (/home/so/Desktop/trash/tensorflow/index.js:1:12)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)

根据我的建议,我尝试这样做:

sudo npm rebuild @tensorflow/tfjs-node build-addon-from-source

但出现此错误:

so@so-notebook:~/Desktop/trash/tensorflow$ sudo npm rebuild @tensorflow/tfjs-node build-addon-from-source

> @tensorflow/tfjs-node@2.0.1 install /home/so/node_modules/@tensorflow/tfjs-node
> node scripts/install.js

CPU-linux-2.0.1.tar.gz
* Downloading libtensorflow
[==============================] 64333/bps 100% 0.0s
* Building TensorFlow Node.js bindings
node-pre-gyp install failed with error: Error: Command failed: node-pre-gyp install --fallback-to-build
node-pre-gyp WARN Using request for node-pre-gyp https download 
node-pre-gyp WARN Tried to download(403): https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v6/2.0.1/CPU-linux-2.0.1.tar.gz 
node-pre-gyp WARN Pre-built binaries not found for @tensorflow/tfjs-node@2.0.1 and node@14.4.0 (node-v83 ABI, glibc) (falling back to source compile with node-gyp) 
In file included from ../binding/tfjs_backend.cc:18:0:
../binding/tfjs_backend.h:25:10: fatal error: tensorflow/c/c_api.h: No such file or directory
 #include "tensorflow/c/c_api.h"
          ^~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Release/obj.target/tfjs_binding/binding/tfjs_backend.o] Error 1
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:194:23)
gyp ERR! stack     at ChildProcess.emit (events.js:315:20)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)
gyp ERR! System Linux 5.6.9-050609-generic
gyp ERR! command "/home/so/node_modules/node/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "build" "--fallback-to-build" "--module=/home/so/node_modules/@tensorflow/tfjs-node/lib/napi-v6/tfjs_binding.node" "--module_name=tfjs_binding" "--module_path=/home/so/node_modules/@tensorflow/tfjs-node/lib/napi-v6" "--napi_version=6" "--node_abi_napi=napi" "--napi_build_version=6" "--node_napi_label=napi-v6"
gyp ERR! cwd /home/so/node_modules/@tensorflow/tfjs-node
gyp ERR! node -v v14.4.0
gyp ERR! node-gyp -v v5.1.0
gyp ERR! not ok 
node-pre-gyp ERR! build error 
node-pre-gyp ERR! stack Error: Failed to execute '/home/so/node_modules/node/bin/node /usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js build --fallback-to-build --module=/home/so/node_modules/@tensorflow/tfjs-node/lib/napi-v6/tfjs_binding.node --module_name=tfjs_binding --module_path=/home/so/node_modules/@tensorflow/tfjs-node/lib/napi-v6 --napi_version=6 --node_abi_napi=napi --napi_build_version=6 --node_napi_label=napi-v6' (1)
node-pre-gyp ERR! stack     at ChildProcess.<anonymous> (/home/so/node_modules/node-pre-gyp/lib/util/compile.js:83:29)
node-pre-gyp ERR! stack     at ChildProcess.emit (events.js:315:20)
node-pre-gyp ERR! stack     at maybeClose (internal/child_process.js:1051:16)
node-pre-gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:287:5)
node-pre-gyp ERR! System Linux 5.6.9-050609-generic
node-pre-gyp ERR! command "/home/so/node_modules/node/bin/node" "/home/so/node_modules/@tensorflow/tfjs-node/node_modules/.bin/node-pre-gyp" "install" "--fallback-to-build"
node-pre-gyp ERR! cwd /home/so/node_modules/@tensorflow/tfjs-node
node-pre-gyp ERR! node -v v14.4.0
node-pre-gyp ERR! node-pre-gyp -v v0.14.0
node-pre-gyp ERR! not ok 

@tensorflow/tfjs-node@2.0.1 /home/so/node_modules/@tensorflow/tfjs-node

新操作系统是:

so@so-notebook:~/Desktop/trash/tensorflow$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 18.04.4 LTS
Release:    18.04
Codename:   bionic

谢谢。

1 个答案:

答案 0 :(得分:2)

在Ubuntu 20.04上,我使用以下方法解决了该问题:

  1. sudo apt install build-essential
  2. sudo npm i -g node-pre-gyp
  3. npm rebuild @tensorflow/tfjs-node build-addon-from-source