To make a long story short, I had a working script which had a “vers” object preloaded (in the way the electron tutorial told me to) and then had properties of the vers object displayed in HTML via my renderer. Somewhere along the line for some unknown reason my code went haywire, my BrowserWindow turned into browserwindow and some ipcMains turned into ipcmains all in my main.js script, for that reason when also the vers object was suddenly gone in my renderer.js Uncaught ReferenceError: vers is not defined I assumed it was also a capitalization mistake, but for the life of me I can’t find it. I’ve cross checked every bit of code related to the preloader file and it’s all exactly as in the tutorial. VS code even autocompletes vers.chrome() for example, but when running the program it just returns an error in the console.
Code below (I’ve left out irrelevant code, ask me to add more if need be):
main.js:
const createWindow = () => {
const win = new BrowserWindow({
width: 800,
height: 600,
minwidth: 800,
minheight: 600,
resizable: true,
frame: false,
webpreferences: {
preload: path.join(__dirname, "preload.js"),
nodeintegration: true,
},
});
win.loadFile("index.html");
};
preload.js:
const { contextBridge } = require("electron");
contextBridge.exposeInMainWorld("vers", {
node: () => process.versions.node,
chrome: () => process.versions.chrome,
electron: () => process.versions.electron,
});
renderer.js:
const information = document.getElementById("info");
information.innerText = `This app uses:n Chrome (v${vers.chrome()}),n
Node.js (v${vers.node()}),n
and Electron (v${vers.electron()})`;
index.html
<body>
<div id="info"></div>
</body>
<script src="./renderer.js></script>
As I said I’ve tried to look for any capitalization mistakes (to no avail). I’ve tried to turn off any parts in my renderer.js which references vers which did make the issue go away (but without the functionality of vers ofc). I’ve crosschecked with the original tutorial, the only difference I have is that my object is called vers instead of versions (I did that to make sure I didn’t get confused between the versions in the preload and the versions that would be used in renderer). I also checked online if I could find the answer but I could only find things related to “require” not being defined, nothing like what I have. Hope any of you can find the issue, thanks in advance!
