1

Importing

// Frontend - Vue.js
const tfn = require('@tensorflow/tfjs-node')

then running

npm install --save @tensorflow/tfjs-node

gets me followed error:

npm ERR! node-pre-gyp ERR! stack     at maybeClose (node:internal/child_process:1093: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.22000
npm ERR! node-pre-gyp ERR! command "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\Name\\Desktop\\Team\\my-app\\node_modules\\@mapbox\\node-pre-gyp\\bin\\node-pre-gyp" "install" "--fallback-to-build"
npm ERR! node-pre-gyp ERR! cwd C:\Users\Name\Desktop\Team\my-app\node_modules\@tensorflow\tfjs-node
npm ERR! node-pre-gyp ERR! node -v v16.17.0
npm ERR! node-pre-gyp ERR! node-pre-gyp -v v1.0.9
npm ERR! node-pre-gyp ERR! not ok

What I already found, but didn't help: 1 , 2

Using:
vue@2.7.8 / Python 3.10.6 / nodev16.17.0 /
node-pre-gyp v1.0.9 / Windows 11 / Visual Studio Code

I need tfjs-node to load my local tensorflow model

const tfn = require("@tensorflow/tfjs-node");
const handler = tfn.io.fileSystem("./path/to/your/model.json");
const model = await tf.loadLayersModel(handler);

After installing npm i @tensorflow/tfjs-node@3.18.0 (Suggestion from @Vladimir Mandic):

98% after emitting CopyPlugin

WARNING  Compiled with 4 warnings                                                                                                                                                                 15:06:11
 warning  in ./node_modules/@tensorflow/tfjs-node/dist/index.js

Critical dependency: the request of a dependency is an expression
warning  in ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js

Module not found: Error: Can't resolve 'node-gyp' in 'C:\Users\Name\Desktop\Team\my-app\node_modules\@tensorflow\tfjs-node\node_modules\@mapbox\node-pre-gyp\lib\util'
warning  in ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js

Module not found: Error: Can't resolve 'npm' in 'C:\Users\Name\Desktop\Team\my-app\node_modules\@tensorflow\tfjs-node\node_modules\@mapbox\node-pre-gyp\lib\util'  
warning  in ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib/util/nw-pre-gyp/index.html

Module parse failed: Unexpected token (1:0)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> <!doctype html>
| <html>
| <head>

 @ ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib sync ^\.\/.*$
 @ ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib/node-pre-gyp.js
 @ ./node_modules/@tensorflow/tfjs-node/dist/index.js
 @ ./node_modules/cache-loader/dist/cjs.js??ref--13-0!./node_modules/babel-loader/lib!./node_modules/@vue/cli-plugin-eslint/node_modules/eslint-loader??ref--14-0!./src/components/PublishCode.js?vue&type=script&lang=js&
 @ ./src/components/PublishCode.js?vue&type=script&lang=js&
 @ ./src/components/Publish.vue
 @ ./src/main.js
 @ multi (webpack)-dev-server/client?http://172.16.7.172:80&sockPath=/sockjs-node (webpack)/hot/dev-server.js ./src/main.js
JAdel
  • 1,309
  • 1
  • 7
  • 24
  • You're using Vue and Node here? Or just Vue? By default, Vue doesn't have any Node capabilities, hence why a TF package specific to node may not work here. Also, Python is probably not relevant either or is it a dependency of TF? – kissu Aug 18 '22 at 16:27
  • I am using node.js for the backend and vue.js for the frontend. https://www.npmjs.com/package/@tensorflow/tfjs-node?source=post_page-----f040de7355d8---------------------- says under Windows / Mac OS X Requires Python 2.7? – JAdel Aug 18 '22 at 18:56
  • Still, you're loading a node package in Vue here, right? – kissu Aug 18 '22 at 19:27
  • Yes. Okay then I guess its a problem with the framework? – JAdel Aug 18 '22 at 20:42

2 Answers2

3

installer for @tensorflow/tfjs-node only tries build
if it cannot find and download prebuild binaries for a given platform
(and build process is anything but simple)

in your case, you're on windows x64 and using nodejs v16, so it should be ok

but...pre-build binaries for that platform for tfjs 3.19.0 are not correctly published
(you can check https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/3.19.0/CPU-windows-3.19.0.zip and see the error directly)

tfjs team will fix it soon

but for the time being, i suggest to use tfjs-node 3.18.0 instead
(you can check https://storage.googleapis.com/tf-builds/pre-built-binary/napi-v8/3.18.0/CPU-windows-3.18.0.zip and see its fine)

Vladimir Mandic
  • 813
  • 5
  • 11
  • I have installed version ``npm i @tensorflow/tfjs-node@3.18.0`` and the errors dissapear. But I am getting new problems, which I will add in the question. – JAdel Aug 23 '22 at 13:08
  • I have run this in my backend (node.js) and it worked. Thank you very much! – JAdel Aug 23 '22 at 13:16
  • 1
    @JAdel this new error `warning in ./node_modules/@tensorflow/tfjs-node/node_modules/@mapbox/node-pre-gyp/lib/util/compile.js` indicates there is something really broken with your setup - i suggest deleting `node_modules` and install dependencies from scratch. reason is that `@mapbox` is NOT a dependency of `tfjs-node` for sure, entire path makes no sense. – Vladimir Mandic Aug 23 '22 at 17:28
  • This actually saved me after 3 days of time wasted on stupid tfjs@4 and stupid Windows – Pouria Moosavi Feb 14 '23 at 15:02
0

try installing a different version of tfjs ( ps these worked for me )

"@tensorflow-models/posenet": "^2.2.2",
"@tensorflow/tfjs": "^3.20.0",
  • 1
    Welcome to SO! Although your code snippet may solve the OP problem, there is an already accepted answer that solved the problem providing more details than yours, using the 3.18.0, stating that the 3.19.0 is not working. In this case, the chance that yours claim any attention and be upvoted is too small. And your answer needs some formatting too; try to use `` for `code` – Marcelo Scofano Diniz Oct 27 '22 at 00:04