0

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

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.