-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
createWorker throws exception with option.langPath set in electron #925
Comments
I would agree that the Node.js version should work similarly regardless of whether or not it is being run in Electron, so this condition should be patched if that is not the current behavior. As an aside, is there a compelling reason to use the Node.js version of Tesseract.js rather than the browser version in an Electron application? My understanding was that Electron supports both, however it was considered a security best practice to avoid giving code access to the filesystem with Node.js integration where possible. However, I have minimal experience with Electron so honestly do not understand the tradeoffs well. The browser version of Tesseract.js can definitely be run using entirely offline resources, as seen in this example repo. |
@Balearica Thx for reply. In Electron, there are two types of processes: main process and render process. As I understand it, what you mentioned and the example you provided is using Tesseract.js in the render process. Upon inspecting the code, I found that the environment is 'webworker' and it utilizes However, in my scenario, I need to run Tesseract.js in the main process after receiving an IPC event from the render process, where the environment is detected as 'electron', and it uses 'node-fetch' to load language files, which does not support local file paths. |
@HQidea Can you make a minimal example repo that can be cloned to replicate this issue? I ran the snippet provided in the OP within the |
@Balearica this is the repo that can reproduce the error. I also tried to replicate the error using the repo you provided. However the results are inconsistent. Sometimes the environment in the worker thread is tested as 'electron', and sometimes as 'node'. When the environment is tested as 'node', everything goes well. I'm not sure why. |
@HQidea I was able to reproduce this issue using your repo, and was able to confirm that simply commenting out the special tesseract.js/src/utils/getEnvironment.js Lines 8 to 9 in 83df363
I am hesitant to edit something without fully understanding what it does, so I reviewed the various issues/PRs regarding why a special case for Electron was introduced to begin with. These are linked below.
After reviewing these discussions, I believe Electron was added as a special case because people wanted to use Node.js in an Electron render process. You do not need special logic to use the Node.js version of Tesseract.js in the main Electron process, and you don't need a special logic to use the browser version of Tesseract.js in the render Electron processes. The only "special case" introduced by Electron is the theoretical ability to run Node.js code in what appears to be a browser context ( While my understanding of Electron is limited, my initial thought is that this special case is not something we need to support. Using Node.js in a renderer process appears to be strongly advised against in the Electron docs. Additionally, the browser version of Tesseract.js works perfectly well in Electron renderer processes, and is just as fast. If this understanding is correct, and there is not a compelling reason to (attempt to) support using the Node.js version in a renderer process, then we could just cut out the Electron-specific stuff entirely, which should solve your issue. |
@Balearica I totally agree with you on not using Node.js in a renderer process. |
It's unclear to me whether this could break any existing code, so my inclination is to combine this with any other planned breaking changes and release as v6. |
I had the same problem |
Tesseract.js version (version number for npm/GitHub release, or specific commit for repo)
"tesseract.js": "^5.1.0",
Describe the bug
When using tesseract.js in the electron environment, setting
option.langPath
to a local file path, thecreateWorker
function results in an error.The following code snippet and error message demonstrate the issue:
error:
Upon examining the code, I found that the condition below appears to be arbitrary since in the electron environment, it should be able to load a language file from a local path as well.
tesseract.js/src/worker-script/index.js
Lines 131 to 136 in 83df363
Expected behavior
It should be able to load a language file from a local path in the electron environment.
The text was updated successfully, but these errors were encountered: