#fork from child process how to target file?

1 messages · Page 1 of 1 (latest)

swift egret
#

How can I target my file with fork?

const { fork } = require('child_process');
...
const workerFilePath = path.join(WhatShouldIAddHere, 'worker.js');
...

any idea?

raven flame
#

absolute path to file

swift egret
#

can I do like
./worker.js they are in the same path

raven flame
#

no

swift egret
#

Thank you, it's working, but have a question
const workerFilePath = path.join('./src/main/', 'worker.js')
this works for linux also ? (it convert?)

raven flame
#

with this path no

#

and it will not work in production

swift egret
#

hmmmm ... do i need to use app.path? something like that?

raven flame
#

you need to use __dirname

swift egret
raven flame
#

you dont have files in production folder

swift egret
#

i'm testing with npm run dev

swift egret
raven flame
#

i dont know

#

its question to you

#

how you move them

swift egret
#

i didn't move anything...
let's start from the beginning

i created this to test a fork

./index.js

const { fork } = require('child_process')

  ipcMain.on('start-task', () => {
    const numbers = Array.from({ length: 10 }, (_, index) => index + 1)
    const clusters = 3 // Number of clusters/workers

    const numberChunks = []
    const chunkSize = Math.ceil(numbers.length / clusters)

    for (let i = 0; i < numbers.length; i += chunkSize) {
      const chunk = numbers.slice(i, i + chunkSize)
      numberChunks.push(chunk)
    }


    const workerFilePath = path.join(__dirname, 'worker.js')

    for (let i = 0; i < numberChunks.length; i++) {
      const worker = fork(workerFilePath)
      const chunk = numberChunks[i]
      worker.send({ chunk })
    }
  })

./worker.js

process.on('message', (message) => {
  const { chunk } = message
  console.log('Received chunk:', chunk)
})

npm run dev -> result: Cannot find module \out\main\worker.js

raven flame
#

its question to your project structure and packagers/bundlers

#

files didnt magically goes from folder A to folder B

swift egret
#

i created the file... what do you mean with folder A and B? I used Electron Vite to create the project, I don't know if that helps

raven flame
#

you also need to setup vite

#

for new files

#

so he process worker.js file as well

#

i am not using vite so i cant help you with it

swift egret
#

But I have more files and they work properly , my utility folder have functions inside and all it's working :/

raven flame
#

because you require them

swift egret
#

owww .... thanks, i will try to find a solution

raven flame
#

with worker you did not

#

you use dynamic path

swift egret
#

correct

swift egret
#

Still looking for help, in case someone have an idea

#

found the solution