Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

495
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda