I thought that electron bundles node together with the required native modules,
It does, but having a browser runtime and a node.js runtime bundled together still leaves you with two different runtimes.
Understanding the difference between the Main and Renderer processes is essential for Electron development.
The Main process runs on the Node.js runtime and has access to Node.js APIs (like net
).
The Renderer process runs on Chromium and has access to browser APIs (like the DOM).
You can't access net
from your client-side Vue code because it is running in the Renderer process.
You need to create a service in the Main process and exchange data between it and something running in the Renderer.
This is typically done using the IPC apis.
// In main process.
const { ipcMain } = require('electron')
ipcMain.on('message-from-browser', (event, arg) => {
const something = doSomethingWithNetModule()
event.reply('reply-to-browser', something)
})
and
// In renderer process (web page).
const { ipcRenderer } = require('electron')
ipcRenderer.on('asynchronous-reply', (event, arg) => {
doSomethingWithDataFromNetModule(arg)
})
ipcRenderer.send('message-from-browser', 'do something with the net module please')
Note that since you are using Webpack you will have issues with require("electron")
which are explained in this question.