提前道歉,因为我确信这些都是很琐碎的事情——我正处于学习前端开发的"你好世界"阶段.

我有一个hello world vite应用程序,我通过以下方式创建并运行:

npm init @vitejs/app
cd hello-vite
npm install npm run dev

我可以在浏览器中查看输出的localhost个url.

我还有一个简单的脚本,可以导入tensorflow并对其进行处理:

$ cat test.mjs 
import * as tf from '@tensorflow/tfjs-node'

const a = tf.tensor([[1, 2], [3, 4]]);
a.print();

$ node test.mjs 
2022-06-27 22:04:16.968270: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 AVX512F FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Tensor
    [[1, 2],
     [3, 4]]

现在我想在我的hello-vite应用程序中获得这test.mjs个行为.所以我try 了以下方法:

$ cat main.ts 
import './style.css'
import * as tf from '@tensorflow/tfjs-node'

const a = tf.tensor([[1, 2], [3, 4]]);
a.print();

document.querySelector('#app').innerHTML = `
  <h1>Hello Vite!!!</h1>
  <a href="https://vitejs.dev/guide/features.html" target="_blank">Documentation</a>
`

但当我跑npm run dev时,它似乎并不开心,并且对aws的东西发出看似无关的抱怨:

$ npm run dev

> hello-vite@0.0.0 dev
> vite


  vite v2.9.12 dev server running at:

  > Local: http://localhost:3000/
  > Network: use `--host` to expose

  ready in 112ms.

✘ [ERROR] Could not resolve "mock-aws-s3"

    node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28:
      43 │     const AWSMock = require('mock-aws-s3');
         ╵                             ~~~~~~~~~~~~~

  You can mark the path "mock-aws-s3" as external to exclude it from the bundle, which will remove
  this error. You can also surround this "require" call with a try/catch block to handle this
  failure at run-time instead of bundle-time.

✘ [ERROR] Could not resolve "aws-sdk"

    node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:76:22:
      76 │   const AWS = require('aws-sdk');
         ╵                       ~~~~~~~~~

  You can mark the path "aws-sdk" as external to exclude it from the bundle, which will remove this
  error. You can also surround this "require" call with a try/catch block to handle this failure at
  run-time instead of bundle-time.

✘ [ERROR] Could not resolve "nock"

    node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23:
      112 │   const nock = require('nock');
          ╵                        ~~~~~~

  You can mark the path "nock" as external to exclude it from the bundle, which will remove this
  error. You can also surround this "require" call with a try/catch block to handle this failure at
  run-time instead of bundle-time.

10:06:42 PM [vite] error while updating dependencies:
Error: Build failed with 3 errors:
node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:43:28: ERROR: Could not resolve "mock-aws-s3"
node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:76:22: ERROR: Could not resolve "aws-sdk"
node_modules/@mapbox/node-pre-gyp/lib/util/s3_setup.js:112:23: ERROR: Could not resolve "nock"
    at failureErrorWithLog (/home/dshin/vite_scratch/hello-vite/node_modules/esbuild/lib/main.js:1605:15)
    at /home/dshin/vite_scratch/hello-vite/node_modules/esbuild/lib/main.js:1251:28
    at runOnEndCallbacks (/home/dshin/vite_scratch/hello-vite/node_modules/esbuild/lib/main.js:1034:63)
    at buildResponseToResult (/home/dshin/vite_scratch/hello-vite/node_modules/esbuild/lib/main.js:1249:7)
    at /home/dshin/vite_scratch/hello-vite/node_modules/esbuild/lib/main.js:1358:14
    at /home/dshin/vite_scratch/hello-vite/node_modules/esbuild/lib/main.js:666:9
    at handleIncomingPacket (/home/dshin/vite_scratch/hello-vite/node_modules/esbuild/lib/main.js:763:9) 
    at Socket.readFromStdout (/home/dshin/vite_scratch/hello-vite/node_modules/esbuild/lib/main.js:632:7)
    at Socket.emit (events.js:314:20)
    at addChunk (_stream_readable.js:297:12)
Vite Error, /node_modules/.vite/deps/@tensorflow_tfjs-node.js?v=0ea0383e optimized info should be defined

如果我注释掉print()个调用和它之前的行,错误就会消失.

我做错了什么?我如何理解这些错误?

推荐答案

我不知道太多,但@tensorflow/tfjs-node似乎只是一个 node 项目,在浏览器中不起作用.

例如,node-pre-gyp是 node 的C++绑定库,在浏览器中肯定不起作用.

你可以试着做npm install mock-aws-s3个和其他出错的库,看看这能让你走多远,但可能没有帮助.

Javascript相关问答推荐

从PWA中的内部存储读取文件

传递一个大对象以在Express布局中呈现

嵌套异步JavaScript(微任务和macrotask队列)

在这种情况下,如何 for each 元素添加id?

在网页上添加谷歌亵渎词

在Three JS中看不到补间不透明度更改效果

加载背景图像时同步旋转不显示的问题

使用Java脚本导入gltf场景并创建边界框

有条件重定向到移动子域

将基元传递给THEN处理程序

FileReader()不能处理Firefox和GiB文件

在将元素追加到DOM之前,createElement()是否会触发回流?混淆abt DocumentFragment行为

TabNavigator和StackNavigator之间的Reaction Native中的导航问题

在Puppeteer中使用promise进行日志(log)记录时出现TargetCloseError

为什么这个最小Angular 的Licial.dev设置不起作用?

更新文本区域行号

如何处理不带参数的redux thunk payloadCreator回调函数?

需要从对象生成列表

带有JS模块模式的Rails的Importmap错误:";Net::ERR_ABORTED 404(未找到)&Quot;

如何将.jsx转换为.tsx