0

I'm new to Nodejs, So help me with this Code

<script>
const os = require('os');
var nam = os.hostname();
document.getElementById('operate').innerHTML=nam;
</script>

<div style="float:left;" id="operate"></div>

Im trying to get nodejs module right inside this html code. Thanks in Advance!

Deadron
  • 5,135
  • 1
  • 16
  • 27
itsdevthen
  • 49
  • 8
  • You need to run your app on server. https://stackoverflow.com/questions/9670222/execute-a-nodejs-script-from-an-html-page – kat1330 Apr 25 '18 at 20:35
  • 2
    Possible duplicate of [execute a Nodejs script from an html page?](https://stackoverflow.com/questions/9670222/execute-a-nodejs-script-from-an-html-page) – kat1330 Apr 25 '18 at 20:38

1 Answers1

0

You cannot execute code against the nodejs API inside a web browser. The nodejs api is only available when being executed by the nodejs VM.

Assuming node js is installed locally and available on the PATH.

  1. Create index.js
  2. Input the following

    const os = require('os'); console.log(os.hostname());

  3. Run the js file node index.js

Deadron
  • 5,135
  • 1
  • 16
  • 27
  • You want to say that you cannot use node modules in web apps? – kat1330 Apr 25 '18 at 20:36
  • That is not what I said. You can in fact use modules inside a client side web app if you are using something like webpack/browserify to resolve imports/requires. This does not mean you have access to the nodejs api(although some of the api is reimplemented/repackageed as npm modules). – Deadron Apr 25 '18 at 20:38