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

492
Views
Is there a way of solving this typeError: connection.connect is not a function in nodejs?

I am trying to connect my school project to a mysql database but I always get the error "TypeError: connection.connect is not a function". I am not quite sure how to fix it. I hope someone is able to solve this.

My index.js

const express = require('express');
const app = express();
var connection = require('./database');

app.use(express.static('public'));

app.get('/form', (req,res) =>{
    res.sendFile(__dirname + '/public/index.html' );
    console.log(req.url);
    console.log(req.path);
})

app.listen(4000, () =>{
    console.log("Server listening on port 4000");
    connection.connect((err) => {
        if(err) throw err;
        console.log("Connected");
    })
});

my database.js

let mysql = require('mysql');

module.exports = () => {
    return mysql.createConnection({
    host: 'localhost',
    database: 'minigames',
    user: 'root',
    password: 'root'
})}```
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

connection is the value exported from the database module.

The value you assigned to module.exports there is a function that you defined.

It is a plain, ordinary function and you have no assigned a connect property to it.

I'm guessing that connect is a property on the return value of mysql.createConnection, but if you want to access that object then, you need to call the function you defined in order to get that object.

about 4 years ago · Juan Pablo Isaza Report

0

const express = require('express');
const app = express();
var connection = require('./database');


app.listen(4000, () =>{
    console.log("Server listening on port 4000");
    connection().connect((err) => {
        if(err) throw err;
        console.log("Connected");
    })
});
about 4 years ago · Juan Pablo Isaza Report
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!