question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:6

github_iconTop GitHub Comments

4reactions
MiLaDTechScommented, Feb 8, 2022

this is still an issue with worker in electron

0reactions
Takayuki-Suzukicommented, Nov 11, 2022

I’m facing the same issue

Read more comments on GitHub >

github_iconTop Results From Across the Web

Electron.js: main process throws "Error: Cannot find module in ...
I have a worker_threads worker that I'm invoking as shown below. It works in dev but throws the following error in production build:...
Read more >
[Solved ]Build successful but Electron app is missing at least a ...
I am building an Electron app and the build process is successful: ... in the main process Uncaught Exception: Error: Cannot find module...
Read more >
Cannot find module 'D:/BabylonJS/1/build/src/scenes/tools.js'
Module._resolveFilename (C:\Users\USER\AppData\Local\Programs\BabylonJS Editor\resources\app.asar\build\src\renderer\module.js:91:12) at ...
Read more >
MongoDB Atlas App Services & Realm
I get the following in my webpack output: WARNING in ./node_modules/realm/lib/utils.js Module not found: Error: Can't resolve 'react-native' ...
Read more >
Error: Cannot find module 'cc' - Cocos Creator
C:\CocosDashboard_1.1.1\resources.editors\Creator\2.4.9\resources\app.asar\editor-framework\lib\renderer\index.ccc.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found