#module.exports returns undefined variable.
58 messages · Page 1 of 1 (latest)
index.js
module.exports.configPath = "hello";
app.js
console.log("1")
const { configPath } = require("./index.js");
console.log("2")
console.log(configPath)
document.getElementById('username').innerText = configPath;
console.log("3")
I have search allot for a solution, but gave up. Left this for around 3 weeks. Now I am back trying to find a solution.
this nodejs api disabled in renderer by default for security purpose
What should I do then?
depends on why you need nodejs api for that
I am doing a login system. When a person logins, I want the variable username in index.js to be exported or shared to app.js to display the username on app.html
Which one is the easiest
depends on your knowledge in web
That was already enabled.
No
const {app, BrowserWindow, Menu, ipcMain} = require('electron')
const path = require('path')
const url = require('url')
const shell = require('electron').shell
const ipc = ipcMain
let win
function createWindow() {
win = new BrowserWindow({width: 500, height: 700, frame:false, autoHideMenuBar: true, resizable: false, webPreferences: {nodeIntegration: true, enableRemoteModule: true, contextIsolation: false}})
So
So it was already enabled
I got this when it was already enabled
When you set value for that variable?
What do you mean exactly
Do you have value set when you require it?
Yes. I have value set to "hello" then I require it.
Show index.js
async function login() {
const uri = "censored";
const client = new MongoClient(uri);
var email = document.getElementById("email-input");
var password = document.getElementById("password-input");
email.blur()
password.blur()
if (email.value == "" || password.value == "") {
document.getElementById("invalid").style.visibility = "visible";
return;
}
const email_result = await client.db("accounts").collection("credentials").findOne({email: email.value});
if (email_result == null) {
const username_result = await client.db("accounts").collection("credentials").findOne({username: email.value});
if (username_result == null) {
document.getElementById("invalid").style.visibility = "visible";
return;
}
else {
var db_username = username_result["username"];
var db_password = username_result["password"];
if (db_username == email.value && db_password == password.value) {
const creds = {
username: db_username,
};
module.exports.configPath = "hello";
//export var omg = "hello";
ipc.send('mainpage')
}
else {
document.getElementById("invalid").style.visibility = "visible";
return;
}
}
}
else {
var db_email = email_result["email"];
var db_password = email_result["password"];
if (db_email == email.value && db_password == password.value) {
var db_username = email_result["username"];
const creds = {
email: db_username,
};
module.exports.configPath = "hello";
//export var omg = "hello";
ipc.send('mainpage')
}
else {
document.getElementById("invalid").style.visibility = "visible";
return;
}
}
}```
This is the part when I set the value of the variable
You cant mutate modules.exports after you require that file
If you want to get it use function
index.js
function username_function() {
username_return = username;
return username_return;
}
module.exports.username_function = username_function;
app.js
console.log("1")
const { username_function } = require("./index.js");
console.log("2")
console.log(username_function)
document.getElementById('username').innerText = username_function;
console.log("3")
Still got undefined. I am sorry if I did something wrong. I still haven't use functions before as I am new to javascript.
When you add function to exports?
What do you mean by that?
I used it here. I just removed the variable and put the function instead.
Look, to be honest. I understood nothing. 🙂
How to do so?
const index = require()
I put this in app.js?
node:internal/errors:465 Uncaught TypeError: The "id" argument must be of type string. Received undefined
at __node_internal_captureLargerStackTrace (node:internal/errors:465:5)
at new NodeError (node:internal/errors:372:5)
at validateString (node:internal/validators:120:11)
at Module.require (node:internal/modules/cjs/loader:1005:3)
at require (node:internal/modules/cjs/helpers:102:18)
at app.js:29:15
app.js
console.log("1")
//const { hello } = require("./index.js");
const index = require();
console.log("2")
console.log(hello)
document.getElementById('username').innerText = hello;
console.log("3")
index.js
module.exports.hello = true;