Rajan Lagah
Rajan Lagah

Reputation: 2528

Import package in javascript file which is script for html

I am doing electron project. In it every tutorial i have watched is adding js in <script> tag i want to separate the js code from html. So i

...
    <script type="text/javascript" src="./../scripts/upload.js" ></script>
...
        <input onchange="upload()" id='train-button' type="file">File</input>
....

and in js file i

function upload(){
    const electron = require('electron');
    const {ipcRenderer} = electron;

    if (x.files.length == 0) {
        alert("Select one or more files.");
    }else{
        ipcRenderer.send("saveFile",targetFile,destinationPath)
    }
}

Now here i am getting error that

Uncaught ReferenceError: require is not defined

but if i define require outside the upload function then it is not running that line and saying

electron is undefined

Upvotes: 0

Views: 73

Answers (1)

reZach
reZach

Reputation: 9489

Try setting nodeIntegration:true in your BrowserWindow config. Doing so will enable require in your js file.

However, this is very poor security practice. I recommend using a security-focused template.

This post might also help explain to you more about best-practices in electron apps regarding using IPC between renderer/main processes.

Upvotes: 1

Related Questions