0

I am new to electron-vite with react. My project has a http node server process. All related files are place in server named folder. The vite folder structure is main, renderer and preload. I have placed server folder in src, that means it is main, renderer, preload and server folder. server.js is there in server folder. Below is the line of code which fails

serverProcess = fork(path.join(__dirname, '../sync/server.js'), {
    env: { PORT: 9090 }
  })

it fails with, Cannot find module 'D:\codebase\syncer\out\server\server.js package.json has "main": "./out/main/index.js", It tries to find the server code in out folder. I don't know what to mention (configuration) to use all folder and files in server folder. The platform is windows 11. please help me.

1 Answer 1

0

One approach is copying the server folder into the final build using extraResources in the electron-builder config:

"build": {
  "extraResources": [
    { "from": "server", "to": "server" }
  ]
}

The script can then be referenced by checking if it’s development or production:

const isDev = process.env.NODE_ENV === 'development';
const serverPath = isDev
  ? path.join(__dirname, '../server/server.js')
  : path.join(process.resourcesPath, 'server', 'server.js');

const serverProcess = fork(serverPath, { env: { PORT: 9090 } });

That way, server.js is included in the build, preventing the “Cannot find module” error.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.