Skip to content

Errors using require() and import #2

@omundy

Description

@omundy

I encountered issues attempting to use this project inside two different projects (Astro as well as Express). You may make notes about potential project or archive so it doesn't happen to someone else.

Bare bones example. While connection works on its own, exporting it as a module fails. This will generate TypeError: mysql.getInstance is not a function error.

// server.js
(async function () {
  var MySQLData = require('./mysql-data.js')
  let data = await MySQLData.getData()
  console.log(data)
})()
// mysql-data.js
var mysql = require('nodejs-mysql')

exports.getData = async () => {
  const config = {
    host: 'localhost',
    port: 3306,
    user: 'root',
    password: '',
    database: 'db_name',
  }

  const db = mysql.getInstance(config)
  let data
  await db
    .exec('select * from table_name')
    .then((rows) => (data = rows))
    .catch((err) => console.warn(err))
  return data
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions