0

I have code like:

<!DOCTYPE html>
<html>
<body>

<h1>HTML DOM Events</h1>
<h2>The onclick Event</h2>

<p>The onclick event triggers a function when an element is clicked on.</p>
<p>Click to trigger a function that will output "Hello World":</p>

<button onclick="myFunction()">Click me</button>

<p id="demo"></p>

<script>
function myFunction() {
  const os = require('os')
  let hostName = os.hostname()
  alert(hostName);
  document.getElementById("demo").innerHTML = "Hello World";
}
</script>

</body>
</html>

The code works when I remove:

  const os = require('os')
  hostName = os.hostname()
  alert(hostName);

I however need to load a module in this function that is being used for the onClick event.

Please note that I am a noob to javascript. Any and all assistance / guidance will be much appreciated.

Thanks.

UPDATE:

for those coming to this page, check: Node-style require for in-browser javascript?

FireHawk2300
  • 91
  • 3
  • 13
  • As per the duplicate, you can’t load a module which depends on Node.js in your browser-side code. If it depends on Node.js then it depends on Node.js. – Quentin Mar 12 '22 at 09:03
  • ahh okay cool, make sense. I will find another way around it. thank you. – FireHawk2300 Mar 12 '22 at 09:11

1 Answers1

-1

 const button = document.getElementById('btn');
 const demo = document.getElementById('demo');
    
    button.onclick = function() {
        demo.innerHTML = 'Hello World';
    };
    
    <button id="btn" >Click me</button>
    <p id="demo"> </p>
    

If you want to load a module you need to show some of your node.js code as we don't see what that looks like and what you want to render.

I suggest reading the documentation of Node.js and EJS.

Jeff
  • 11
  • 4
  • that is it my bud, should load the OS module, if I want to perform some OS functions on button click. – FireHawk2300 Mar 12 '22 at 08:30
  • regarding `I suggest reading the documentation of Node.js and EJS.` what documentation exactly? your answer isn't an answer?... – FireHawk2300 Mar 12 '22 at 08:38
  • https://nodejs.org/api/os.html const os = require('os'); should be placed in the node file. The os module provides operating system-related utility methods and properties. It doesn't do anything on its own. – Jeff Mar 12 '22 at 08:53
  • okay, so where should it be placed if I want to perform OS operations on button click? – FireHawk2300 Mar 12 '22 at 08:56
  • eg: I want to see the hostname when I click the button. `let hostName = os.hostname()` and `alert(hostName);` – FireHawk2300 Mar 12 '22 at 09:01
  • that may not be the exact OS functions, but just as an example to help get a proper answer – FireHawk2300 Mar 12 '22 at 09:02