【问题标题】:Cannot import @tensorflow/tfjs-node in nodejs无法在 nodejs 中导入 @tensorflow/tfjs-node
【发布时间】:2019-08-17 14:58:43
【问题描述】:

无法将@tensorflow/tfjs-node 导入我的程序。 我试图按如下方式导入它:

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

并收到如下错误:

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)

但是当我尝试将@tensorflow/tfjs 导入为

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

没有遇到错误。

我使用 npm as 安装了@tensorflow/tfjs-node

sudo npm install @tensorflow/tfjs-node

然后得到一个输出(如下):

@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```

【问题讨论】:

  • @Thomas Dondorf,感谢您的支持!

标签: node.js tensorflow.js


【解决方案1】:

这里有两个问题。

以 root 身份安装包

您想以 root 身份安装依赖项,但默认情况下 npm 不会以 root 身份运行。通过the following arguments 开启root权限:

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

您还应该重新考虑使用另一个(不是 root)来运行您的脚本。

Tensorflow.js 需要

tensorflow/tfjs-node 的 require 不会像你那样工作,包 @tensorflow/tfjs-node 不会导出任何东西,只需要使用本机 C++ 绑定。

这是应该怎么做的(引用tutorial):

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

【讨论】:

【解决方案2】:

我遇到了同样的问题,并通过在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

您可能还想尝试使用 1.2.6 的 tfjs-node,我能够工作。

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

【讨论】:

  • 感谢@tomoima525 的支持,我的问题已经解决了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-05-03
  • 2018-12-25
  • 2020-10-21
  • 1970-01-01
  • 2022-08-18
  • 2020-02-25
  • 2021-05-29
相关资源
最近更新 更多