this.util.TextEncoder is not a constructor only in electron app (works in chrome)

本秂侑毒 提交于 2021-02-05 09:11:36

问题


I am creating a body segmentation app using tensorflow bodypix model. It works fine in the browser. I am using webpack to use its modules(see below)

import * as wasm from "@tensorflow/tfjs-backend-wasm";
import * as tf from "@tensorflow/tfjs-core";
import * as bodyPix from "@tensorflow-models/body-pix";

wasm.setWasmPaths("./wasm/");
tf.setBackend("wasm").then(() => {
  //some simple vanilla js code
});
//some more vanilla js code...

It works exactly fine in chrome and giving output as expected after running npx webpack .

However when irun it with electron simply by creating a main electron file it outputs nothing but a blank white screen with the following error in console-

Uncaught TypeError: this.util.TextEncoder is not a constructor
    at new <anonymous> (main.js:2)

the line where it is pointing is from a minified codew which looks like this-

...SOME_CODE...&&Me().setPlatform("node",new class{
constructor(){this.util=n(758),this.textEncoder=new this.util.TextEncoder}...SOME_MORE_CODE...

i thought that electron is simply chrome without top bars, but this seems wrong. can someone help me here i am using following versions-

"nodejs v12.16.3", "electron11.1.1", "tfjs2.8.2"

see the screen shot of chrome and electron-

IN CHROME(click to enlarge)

................................................

IN ELECTRON(click to enlarge)


回答1:


THE SOLUTION

i previously have

wasm.setWasmPaths("./wasm/");
tf.setBackend("wasm").then(() => {
  //some simple vanilla js code
});

in my main code, and i have copied the folder from wasm(dist/) to project's folder.

Deleting the same from my project's folder and changing the code to -

wasm.setWasmPaths("../node_modules/@tensorflow/tfjs-backend-wasm/dist/"); //or start from ./ if your main file is in same folder as node_modules
tf.setBackend("wasm").then(() => {
  //...
});

How i recahed here?

at first thanks to @edkeveked for his effort and pointing me to

Error loading TensorflowJS in Electron App (Nodejs)

i got the solution by creating an electron hello world project and then adding tfjs, then tfjs-backend-wasm. the new project is working correctly but however even moving the node_modules from new project to older one is not working for the older. but as soon i changed the wasm path, it worked giving no error.



来源:https://stackoverflow.com/questions/65562209/this-util-textencoder-is-not-a-constructor-only-in-electron-app-works-in-chrome

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