How to use require('request') in Electron.NET #552

Closed
opened 2026-01-29 16:42:46 +00:00 by claunia · 12 comments
Owner

Originally created by @Hisaack on GitHub (Aug 17, 2020).

Originally assigned to: @GregorBiswanger on GitHub.

Hi everyone, I have ASP CORE Project and I have installed this package of Electron API and it is working fine. I want to add request module but I keep on getting error
Screenshot (190)

I have tried to install the package through npm install request --save on cmd I opened on my project folder but still it does not work.

This is my full codes

var request = require('request');
var fs = require('fs');

function downloadfile (file_url, targetPath) {
   
    // Save variable to know progress
    var received_bytes = 0;
    var total_bytes = 0;

    var req = request({
        method: 'GET',
        uri: file_url
    });

    var out = fs.createWriteStream(targetPath);
    req.pipe(out);

    req.on('response', function (data) {
        // Change the total bytes value to get progress later.
        total_bytes = parseInt(data.headers['content-length']);
    });

    req.on('data', function (chunk) {
        // Update the received bytes
        received_bytes += chunk.length;

        showProgress(received_bytes, total_bytes);
    });

    req.on('end', function () {
        alert("File succesfully downloaded");
        console.log("File succesfully downloaded");
    });
};

function showProgress(received, total) {
    var percentage = (received * 100) / total;
    console.log(percentage + "% | " + received + " bytes out of " + total + " bytes.");
}

Originally created by @Hisaack on GitHub (Aug 17, 2020). Originally assigned to: @GregorBiswanger on GitHub. Hi everyone, I have ASP CORE Project and I have installed this package of Electron API and it is working fine. I want to add request module but I keep on getting error ![Screenshot (190)](https://user-images.githubusercontent.com/40029080/90430608-6fb25900-e0d0-11ea-99fb-62d54860aea1.png) I have tried to install the package through `npm install request --save` on cmd I opened on my project folder but still it does not work. This is my full codes ``` var request = require('request'); var fs = require('fs'); function downloadfile (file_url, targetPath) { // Save variable to know progress var received_bytes = 0; var total_bytes = 0; var req = request({ method: 'GET', uri: file_url }); var out = fs.createWriteStream(targetPath); req.pipe(out); req.on('response', function (data) { // Change the total bytes value to get progress later. total_bytes = parseInt(data.headers['content-length']); }); req.on('data', function (chunk) { // Update the received bytes received_bytes += chunk.length; showProgress(received_bytes, total_bytes); }); req.on('end', function () { alert("File succesfully downloaded"); console.log("File succesfully downloaded"); }); }; function showProgress(received, total) { var percentage = (received * 100) / total; console.log(percentage + "% | " + received + " bytes out of " + total + " bytes."); } ```
claunia added the question label 2026-01-29 16:42:46 +00:00
Author
Owner

@GregorBiswanger commented on GitHub (Aug 17, 2020):

Are you using this with the HostHook feature?
Can you provide a sample project?

@GregorBiswanger commented on GitHub (Aug 17, 2020): Are you using this with the HostHook feature? Can you provide a sample project?
Author
Owner

@Hisaack commented on GitHub (Aug 17, 2020):

Am not using HostHook feature. Sure let me compile and push it to the github

And btw, my Asp Core project is using version 3.0

@Hisaack commented on GitHub (Aug 17, 2020): Am not using HostHook feature. Sure let me compile and push it to the github And btw, my Asp Core project is using version 3.0
Author
Owner

@GregorBiswanger commented on GitHub (Aug 17, 2020):

okay, the problem is, you want to use require within html with javascript (renderer process). You should use the HostHook feature for your own node module packages. Download the API demo app and take a look at the "Execute your own TypeScript code" part.

https://github.com/ElectronNET/electron.net-api-demos/releases/tag/v5.22.13
https://github.com/ElectronNET/electron.net-api-demos

@GregorBiswanger commented on GitHub (Aug 17, 2020): okay, the problem is, you want to use require within html with javascript (renderer process). You should use the HostHook feature for your own node module packages. Download the API demo app and take a look at the "Execute your own TypeScript code" part. https://github.com/ElectronNET/electron.net-api-demos/releases/tag/v5.22.13 https://github.com/ElectronNET/electron.net-api-demos
Author
Owner

@Hisaack commented on GitHub (Aug 17, 2020):

Okay, Let me try, and by the way do Electron.NET Api supports the downloading feature? I wanna download a file from the server and update the progress bar on status progress. I can't see this feature available.

@Hisaack commented on GitHub (Aug 17, 2020): Okay, Let me try, and by the way do Electron.NET Api supports the downloading feature? I wanna download a file from the server and update the progress bar on status progress. I can't see this feature available.
Author
Owner

@GregorBiswanger commented on GitHub (Aug 17, 2020):

See that solution for you: How to download a file in ASP.NET Core

@GregorBiswanger commented on GitHub (Aug 17, 2020): See that solution for you: [How to download a file in ASP.NET Core]( https://stackoverflow.com/questions/45727856/how-to-download-a-file-in-asp-net-core)
Author
Owner

@Hisaack commented on GitHub (Aug 17, 2020):

When I try to add hosthook on command CLI, it refuses. I used this command electronize add hosthook
Screenshot (192)

@Hisaack commented on GitHub (Aug 17, 2020): When I try to add hosthook on command CLI, it refuses. I used this command `electronize add hosthook` ![Screenshot (192)](https://user-images.githubusercontent.com/40029080/90440292-a643a000-e0df-11ea-81be-03772e2830e4.png)
Author
Owner

@GregorBiswanger commented on GitHub (Aug 17, 2020):

Use the command for one time in your project folder... that creates a ElectronHost folder in your project with package.json.. into this use the npm install request. by the way... the best is here to use the native asp.net core download features... use only the Host Hook feature for native JavaScript/TypeScript code with node.js..

@GregorBiswanger commented on GitHub (Aug 17, 2020): Use the command for one time in your project folder... that creates a ElectronHost folder in your project with package.json.. into this use the `npm install request`. by the way... the best is here to use the native asp.net core download features... use only the Host Hook feature for native JavaScript/TypeScript code with node.js..
Author
Owner

@Hisaack commented on GitHub (Aug 17, 2020):

Am sorry again, am trying to invoke this command dotnet electronize add hosthook or electronize add hosthook to create ElectronHost folder and it is not working. Is there anything am missing, yes this is my first time am trying to execute this command.

@Hisaack commented on GitHub (Aug 17, 2020): Am sorry again, am trying to invoke this command `dotnet electronize add hosthook` or `electronize add hosthook` to create ElectronHost folder and it is not working. Is there anything am missing, yes this is my first time am trying to execute this command.
Author
Owner

@Hisaack commented on GitHub (Aug 17, 2020):

Screenshot (192)_LI

This is what am describing here. I dont know why it does not recognize this command. It says unknown command add

@Hisaack commented on GitHub (Aug 17, 2020): ![Screenshot (192)_LI](https://user-images.githubusercontent.com/40029080/90443414-e5c0bb00-e0e4-11ea-97b1-ac4504eb51f1.jpg) This is what am describing here. I dont know why it does not recognize this command. It says `unknown command add`
Author
Owner

@GregorBiswanger commented on GitHub (Aug 17, 2020):

Use the command in your project and use the Electron.NET CLI and API with Version 7.30.2 for .NET Core 3.0 Support.

@GregorBiswanger commented on GitHub (Aug 17, 2020): Use the command in your project and use the Electron.NET CLI and API with Version 7.30.2 for .NET Core 3.0 Support.
Author
Owner

@GregorBiswanger commented on GitHub (Oct 25, 2020):

Did you solve the problem?

@GregorBiswanger commented on GitHub (Oct 25, 2020): Did you solve the problem?
Author
Owner

@GregorBiswanger commented on GitHub (Mar 28, 2023):

🎉🚀 New Electron.NET version 23.6.1 released 🚀🎉

With native Electron 23 and .NET 6 support. Your problem should be fixed here. If you continue to have the problem, please let us know. Please note the correct updating of your API & CLI. Info in the README. Have fun!

@GregorBiswanger commented on GitHub (Mar 28, 2023): 🎉🚀 New Electron.NET version 23.6.1 released 🚀🎉 With native Electron 23 and .NET 6 support. Your problem should be fixed here. If you continue to have the problem, please let us know. Please note the correct updating of your API & CLI. Info in the README. Have fun!
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/Electron.NET#552