Can't find module in app.asar after build
See original GitHub issue- Version: 22.10.5
- Electron Version: 12.0.0
- Electron Type (current, beta, nightly): current
- Target: Windows NSIS
I’m building an application that must check files and update corrupted files. I need to check a few hundred files’ hashes to check their integrity, to split this job in parallel I’m using worker_threads, that suppose to work and in fact works while in development.
The structure is something like this:
main.js modules/ – Tasks.js ---- tasks/ ------fileCheck.js
You can check a snipped here: https://github.com/LeonanCarvalho/electron-quick-start
The Task is invoked from another module requested by the main process, the array of files to check is split into equal parts based on the number of CPU numbers and should be processed in parallel.
Basicaly you can reproduce just calling the worker inside the main process:
const { Worker } = require('worker_threads')
const worker = new Worker('modules\tasks\fileCheck.js')
//This will error that script.js was not found.
After build and install the program triggers this error:
Error: Cannot find module 'C:\Program Files\app\resources\app.asar\modules\tasks\fileCheck.js'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:887:15)
at Function.Module._load (internal/modules/cjs/loader.js:732:27)
at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
at MessagePort.<anonymous> (internal/main/worker_thread.js:174:24)
at MessagePort.[nodejs.internal.kHybridDispatch] (internal/event_target.js:354:41)
at MessagePort.exports.emitMessage (internal/per_context/messageport.js:18:26)
The path C:\Program Files\app\resources\app.asar\modules\tasks\fileCheck.js should be correct since those files should be packed inside app.asar but why can’t be used?
My first WA was try to require the modules inside Task.js, eg. require('./tasks/fileCheck');
, not worked.
The second WA was to create a kind of shadow load, placing the entire content of fileCheck
in a string and use Worker with eval options, but the inside modules were not imported too.
I also tried to unpack it from Asar:
"asarUnpack": [
"**/modules/tasks/**/*"
],
"extraResources": [
{
"from": "modules/tasks/fileCheck.js",
"to": "app.asar.unpacked/modules/tasks/fileCheck.js"
}
]
And to use unpacked I must implement a “isDev” if to work around path structure (sounds wrong)
function getWorkerPath(taskName) {
if(isDev){
return path.resolve(__dirname, `tasks/${taskName}.js`)
}else{
return path.resolve(process.resourcesPath, 'app.asar.unpacked', `${taskName}.js`);
}
}
but I got the same issue as use it as string/eval, doesn’t load the node modules (like http or fs)
ReferenceError: http is not defined
at C:\\Program Files\\app\\resources\\app.asar.unpacked\\downloadFiles.js:47:29
at new Promise (<anonymous>)
at download (C:\\Program Files\\app\\resources\\app.asar.unpacked\\downloadFiles.js:29:16)
at downloadFile (C:\\Program Files\\app\\resources\\app.asar.unpacked\\downloadFiles.js:149:16)
at downloadQueue (C:\\Program Files\\app\\resources\\app.asar.unpacked\\downloadFiles.js:171:9)
at C:\\Program Files\\app\\resources\\app.asar.unpacked\\downloadFiles.js:177:20
at processTicksAndRejections (internal/process/task_queues.js:93:5)'
```
Maybe I'm doing it wrong, but it was the most intuitive way I found to implement it since it works in develop mode.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:6
Top GitHub Comments
this is still an issue with worker in electron
I’m facing the same issue