Error: requests not defined

const https = require('https')

this my code and when I see the console:Annotation 2020-12-31 151205
so what I have to do?

This seems like backend javascript, are you writing this on the frontend?

  <body>
    <script>
      const https = require('https')
      https.get("https://ipgeolocation.abstractapi.com/v1/?api_key=" + process.env.IPAPI,resp => {
            let data = "";

            resp.on("data", chunk => {
              data += chunk;
            });
            
            resp.on("end", () => {
              var IP = JSON.parse(data)["ip_address"]
              var answer = prompt(IP)
            });
          }
        )
        .on("error", err => {
          console.log("Error: " + err.message);
        });
    </script>
  </body>

this is all of the code

Don’t use requests on the frontend, look into Ajax or jquery for HTTP requests.

I want to do it on node js :expressionless:

require() is a NodeJS function to load modules and because NodeJS does not work in the browser, require statements also do not work in the browser. If you still insist on using require in the browser, use RequireJS.

so it means there is no way for it? like defining it manually

You should use ajax
There are ways to use node functions in normal clientside js but you shouldn’t. Ajax will work better

I will do some research about AJAX thanks

You can also use Browserify if you want using require(....) like what NodeJS does in frontend.

#MakeItSimple

1 Like