#module.exports returns undefined variable.

58 messages · Page 1 of 1 (latest)

unique lagoon
#

I am trying to use module.export to export a variable to another Javascript file, but when I console.log that variable. It says undefined.

#

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.

shut crow
#

this nodejs api disabled in renderer by default for security purpose

unique lagoon
#

What should I do then?

shut crow
#

depends on why you need nodejs api for that

unique lagoon
#

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

shut crow
#

you have 3 ways

#

enable nodeIntegraion

#

use webpack

#

or use preload

unique lagoon
#

Which one is the easiest

shut crow
#

depends on your knowledge in web

unique lagoon
shut crow
#

No

unique lagoon
#
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}})

shut crow
#

So

unique lagoon
#

So it was already enabled

unique lagoon
shut crow
#

When you set value for that variable?

unique lagoon
#

What do you mean exactly

shut crow
#

Do you have value set when you require it?

unique lagoon
shut crow
#

Show index.js

unique lagoon
#
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

shut crow
#

You cant mutate modules.exports after you require that file

#

If you want to get it use function

unique lagoon
#

hmm

#

So I should export a function instead of a variable?

shut crow
#

Yes

#

And function should return that value

unique lagoon
#

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.

shut crow
#

When you add function to exports?

unique lagoon
shut crow
#

When you do this

#

?

unique lagoon
shut crow
#

=/

#

You may replace destructuring assignment for module reference

unique lagoon
#

Look, to be honest. I understood nothing. 🙂

shut crow
#

And access variable via that reference

#

Well time to learn some nodejs

#

And js

unique lagoon
shut crow
#

const index = require()

unique lagoon
#

I put this in app.js?

shut crow
#

You replace it

#

And trying to use variable after you do login

unique lagoon
#

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;
shut crow
#

I think you need to read some docs about nodejs

#

And js too

unique lagoon
#

FINNALY

#

IT worked