Request module not working in my project


#1

This maybe a problem or may not but I tried to install npm install request into the my project folder and got a “err code 1” back. In my project I have the node_module folder along with the json package but still don’t know why request (url,function(err,res,body) {}does not work. I know it is this…I tried the request(url,function(err,res,body){content.push(‘hello’)} and when called using alert(content) in the index.html file it did nothing. The alert did not pop up.

If someone understands what is going on please help me solve this problem. my original post is below.

-------my project folder is on my desktop with all files. If this is the problem. let me know----------------

I am still very new to electron but I have a problem that I can not solve. The request is not going out to any website. I am using a windows 10 (x64) machine to make my project.

I wanted to store the body(html) into an array that will be called back in the index.html file. The call back works fine. It has been tested.

Here is the code for the .js file

   function data( callback){
       var content = [];
       var request = require('request');

       request('https://www.google.com', function (error, response, body) {
             if(error){
                  content.push('Error:', error);
             }
             if(response.statusCode !== 200){
                  content.push('Invalid Status Code Returned:', response.statusCode);
             }
                  content.push(body);
                  callback(content);
        });
            
            
     } 
    module.exports = data;

In the html file I call data

    var data require('./data.js')

    data function(content){
       alert(content);
   } 

If I am missing something please give me some advise on it.


#2

The part that you say is in the HTML file doesn’t look syntactically correct to me:

var data require('./data.js')

:point_up: This is invalid JavaScript syntax.

Unfortunately, beyond this, I’m not sure that I can be much help. I haven’t used the request module in quite some time. Also, when trying to use printf-style debugging on things like this, I would much more commonly use console.log than alert. At least with console.log you’ll get some output and know where things broke.


#3

You are correct it is not right. I made an error. it is var data = require(’./data.js’)

This is not the problem though.

I am beginning to think you can not use electron as you would node when using request.

Thanks


#4

Here is the answer to my request module install problem. I had to include the request module inside package.json dependencies.

"dependencies": {
"request": "latest",
"cheerio": "latest"
}