the actual mysql package appears to have a syntax error when used with node.js

So i’m trying to use mysql with nodejs, and when I attempt to use it, I receive a syntax error that appears to originate within the MySQL package itself.

require('/usr/local/bin/mysql')

which returns the exception:

/usr/local/Cellar/mysql/8.0.26/bin/mysql:1
����

SyntaxError: Invalid or unexpected token

Does anyone know why this might be happening?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

  1. init project:
npm init
  1. install mysql package:
npm i --save mysql
  1. connect to db (manual) :
const mysql = require('mysql');
const db = mysql.createConnection({
  host     : 'localhost',
  user     : 'root',
  password : '',
  database : 'databasenamehere'
});
 
db.connect();
 
db.query('SELECT * FROM test_table', (error, rows, fields) => {
  console.log(rows);
});

Method 2

Thanks for the comments, guys. The solution was that I just had to install the latest version of the MySQL module from Github.

npm install mysqljs/mysql

forgot to add: I changed require(‘/usr/local/bin/mysql’) back to require(‘mysql’) after installing the latest version of MySQL as well.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x