Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

482
Views
ReferenceError: Can't find variable: require. How can I fix this error?

I want to export some functions from my database.js file and require them into the other file login.js, which is in the same folder. I tried also to declare the require globally and in a function, both ways did not work. I am using node.js/express.js.

DATABASE.JS

let mysql = require('mysql');

const connection = mysql.createConnection({
    host: 'localhost',
    database: 'minigames',
    user: 'root',
    password: 'root'
});
    
function connected(){
    connection.connect((err) => {
        if(err) throw err;
        console.log("Connected...");
    })
}

function select(attribute){
    return new Promise((resolve, reject) => {
        let sql = `SELECT ${attribute} FROM player`;
        connection.query(sql, (err, result, field) => {
            if(err) reject(err);
            resolve(Object.values(JSON.parse(JSON.stringify(result))));
        })
    })
}

async function query(attribute) {
    return await select(attribute);
}


module.exports = {connection, connected, select, query};

LOGIN.JS

let userList = [];
function checkLogin(){
    const database = require('./database');
    let userListPromise = database.query('username, password');
    userListPromise
    .then()
    .then((values) =>{
        let i = 0;
        while(values[i] != null){
            userList.push(values[i]);
            i++;
        }
        console.log("list filled");
        console.log(userList);
    })
}
checkLogin();
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!