Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, it is possible to display the os.hostname() module from Node.js in an electron application's HTML. You can achieve this by using a renderer process to send the hostname data to the main process and then use the main process to send the data back to the renderer process to be displayed in the HTML.

To do this, you need to create a renderer process that sends a message to the main process requesting the hostname. Here's an example:

// Renderer process
const { ipcRenderer } = require('electron')

ipcRenderer.send('get-hostname')

ipcRenderer.on('send-hostname', (event, data) => {
  document.getElementById('hostname').textContent = data
})

In the main process, you need to listen for the 'get-hostname' message, retrieve the hostname using os.hostname(), and then send it back to the renderer process. Here's an example:

// Main process
const { ipcMain } = require('electron')
const os = require('os')

ipcMain.on('get-hostname', (event) => {
  event.sender.send('send-hostname', os.hostname())
})

Finally, you just need to add an element in your HTML with an ID of 'hostname' where the data will be displayed:

<!-- HTML -->
<body>
  <h1>Hostname: <span id="hostname"></h1>

  <script src="renderer.js"></script>
</body>

Now, when you run your electron app, the hostname will be displayed in the HTML.