Cannot import @tensorflow/tfjs-node in nodejs

落爺英雄遲暮 提交于 2019-12-24 04:49:07

问题


Cannot import @tensorflow/tfjs-node into my program. I was trying to import it as follows:

const tf = require('@tensorflow/tfjs-node')

And received an error as follows:

internal/modules/cjs/loader.js:638
    throw err;
    ^

Error: Cannot find module '/home/nama/working/Canvas-WEB/node_modules/@tensorflow/tfjs-node/lib/napi-v4/tfjs_binding.node'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
    at Function.Module._load (internal/modules/cjs/loader.js:562:25)
    at Module.require (internal/modules/cjs/loader.js:692:17)
    at require (internal/modules/cjs/helpers.js:25:18)
    at Object.<anonymous> (/home/nama/working/Canvas-WEB/node_modules/@tensorflow/tfjs-node/dist/index.js:44:16)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
    at Function.Module._load (internal/modules/cjs/loader.js:585:3)

But when i try to import @tensorflow/tfjs as

const tf = require('@tensorflow/tfjs')

No error was encountered.

I installed @tensorflow/tfjs-node using npm as

sudo npm install @tensorflow/tfjs-node

Then got an output (follows):

@tensorflow/tfjs-node@1.2.7 install /home/nama/working/Canvas-WEB/node_modules/@tensorflow/tfjs-node


node scripts/install.js

CPU-linux-1.2.7.tar.gz
(node:29606) UnhandledPromiseRejectionWarning: Error: EACCES: permission denied, mkdir '/home/nama/working/Canvas-WEB/node_modules/@tens
orflow/tfjs-node/deps'
(node:29606) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async 
function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:29606) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not h
andled will terminate the Node.js process with a non-zero exit code.
npm WARN canvas-web@1.0.0 No repository field.

+ @tensorflow/tfjs-node@1.2.7
updated 1 package and audited 276 packages in 12.428s
found 0 vulnerabilities```

回答1:


There are two problems here.

Installation of the package as root

You want to install the dependencies as root but npm will not run as root by default. Pass the following arguments to enable root privileges:

sudo npm install @tensorflow/tfjs-node --unsafe-perm=true --allow-root

You should also reconsider using another (not root) to run your script.

Tensorflow.js require

The require of tensorflow/tfjs-node will not work the way you do it, the package @tensorflow/tfjs-node will not export anything and is only required to use the native C++ bindings.

Here is how it should be done (quote from the tutorial):

const tf = require('@tensorflow/tfjs');
require('@tensorflow/tfjs-node');



回答2:


I had the same issue and solved it by running the command below at node_modules/@tensorflow/tfjs-node

$ cd node_modules/@tensorflow/tfjs-node/
$ npm install
> @tensorflow/tfjs-node@1.2.6 install /Users/tomoima525/workspace/javascript/tensorflow/baseball/node_modules/@tensorflow/tfjs-node
> node scripts/install.js

CPU-darwin-1.2.7.tar.gz
* Building TensorFlow Node.js bindings

Also you might want to try with 1.2.6 of tfjs-node, which I was able to work.

https://github.com/tensorflow/tfjs-node#mac-os-x-requires-xcode



来源:https://stackoverflow.com/questions/57537386/cannot-import-tensorflow-tfjs-node-in-nodejs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!