Node js Site Not Working

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="description" content="A cool thing made with Glitch" />
    <script src="/script.js" defer></script>
    <title>Welcome to Glitch!</title>
    <script src="/script.js"></script>
    <link rel="stylesheet" href="/style.css" />
  </head>
  <body>
    <script>
      var randomnumber = require("random-number");
      console.log(randomnumber())
      const Form = document.querySelector("form");
      var options = {
        min: 0,
        max: 1000,
        integer: true
      };

      document.write("hi");
      event.preventDefault();
      document.write(randomnumber(options));
    </script>
  </body>
</html>

Why is the script tag not working?

Heh heh. Your like most coders - and I hate to break it to you - that’s not how you do it! You may only use Node.js on a server file, not on the actual website. You might’ve thought that EMCAscript had this. They don’t, but there are types like in TypeScript. You will have to send data through, or use this

var maxNumber = 10 // or anything, really
var random_number = Math.floor(Math.random() * maxNumber);

you could also do

function randomNum(num) {
return Math.floor(Math.random() * num); // every time the function is ran, return a random value
}

and use it like

var num = randomNum(100); // let's say the value is 67
alert(num) // alerts 67 to the user
4 Likes

If you want to use that package you should check out a bundler like parcel (simpler) or webpack. There are also other options with names I forgot. Bundling some modules can cost a lot and some modules cannot be bundled. You can check for those things using this website
Pretty sure a random number function should require no apis unless it’s calling a system random number function

1 Like

Or browserify.

Or… re-write this in plain JS.

  1. Do you know about DOM selectors like getElementById and getElementsByClassName? Don’t use document.write - it adds unsafed code that people can use to insert viruses. Try making an element and writing to that with .innerText.
  2. Making a random number generator in JS is easy. I gather you know basic javascript - with your skills, you should be able to put the output of your function into the website.

Best of luck!