node-machine-id icon indicating copy to clipboard operation
node-machine-id copied to clipboard

Can't resolve 'child_process'

Open farhaan008 opened this issue 4 years ago • 2 comments

On importing import {machineId, machineIdSync} from 'node-machine-id' in app component then getting this error. Any work around for the same. ERROR in ./node_modules/node-machine-id/dist/index.js Module not found: Error: Can't resolve 'child_process' in 'C:\Users\farhan\Desktop\ Farhan\Projects\BFMC\BFMClient\node_modules\node-machine-id\dist'

farhaan008 avatar Sep 15 '20 15:09 farhaan008

I had the same problem, I think. You are probably using machineIdSync in the Electron renderer process? child_process is a node module and can only be accessed from the main process.

Try a request from the renderer process:

this.userId = this.electronService.ipcRenderer.sendSync('UUID_REQUEST')

And in the main process:

ipcMain.on('UUID_REQUEST', async (event, args) => {
    event.returnValue = machineIdSync();
});

MarkusSvensson avatar Oct 30 '20 15:10 MarkusSvensson

Faced with the same problem I did some research. With inspiration from https://stackoverflow.com/questions/54459442/module-not-found-error-cant-resolve-child-process-how-to-fix I solved it for the rendering process by adding following config for webpack.config.js. After this change I get a machine id when running a build electron app, when running "serve" it fails with an exception at execution but that is ok for me. node: { child_process: "empty" }

MarkusSvensson avatar Nov 01 '20 15:11 MarkusSvensson