开发者问题收集

为什么导入 tfjs-node 时会出现错误?

2022-08-17
276
import * as tfn from '@tensorflow/tfjs-node

抛出了以下警告:

WARNING  Compiled with 4 warnings                                                                                                                                                                 15:06:46  
warning  in ./node_modules/@tensorflow/tfjs-node/dist/index.js    
Critical dependency: the request of a dependency is an expression    
warning  in ./node_modules/@mapbox/node-pre-gyp/lib/util/compile.js   
Module not found: Error: Can't resolve 'node-gyp' in 'C:\Users\Username\Desktop\Team\my-app\node_modules\@mapbox\node-pre-gyp\lib\util'   
warning  in ./node_modules/@mapbox/node-pre-gyp/lib/util/compile.js    
Module not found: Error: Can't resolve 'npm' in 'C:\Users\Username\Desktop\Team\my-app\node_modules\@mapbox\node-pre-gyp\lib\util'   
warning  in ./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>

为什么会这样? (vue: 2.7.8)

Webpack.config.js:

const path = require('path');
const nodeExternals = require('webpack-node-externals');

module.exports = {
  entry: './server.js',
  mode: 'production',
  target: 'node',
  externals: [nodeExternals()],
  output: {
    path: path.resolve(__dirname, '.'),
    filename: 'server.bundle.js'
  }
};

软件包版本:

├── @tensorflow/[email protected]
├── @tensorflow/[email protected]
├── @tensorflow/[email protected]
├── @tensorflow/[email protected]
├── [email protected]

(如果您需要更多详细信息,请告诉我)

还尝试了 Windows Subsystem for Linux 的解决方法,但出现了以下错误:

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! @tensorflow/[email protected] install: `node scripts/install.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the @tensorflow/[email protected] install script.
1个回答

根据您的错误,您很可能使用的是 Python 3。根据 文档 ,“Windows 和 OSX 对 node-gyp 的构建支持需要 Python 2.7”。如果您使用的是正确的 Python 版本,请参阅 Windows 故障排除指南 以获取更多故障排除信息。

相反,您可以使用 适用于 Linux 的 Windows 子系统 ,然后通过 Linux 与 TensorFlow.js 交互。我团队中使用 Windows 的同事更喜欢这种方法,因此我推荐它。

Jen Person
2022-08-17