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

Time:10-05

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?

CodePudding user response:

  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);
});

CodePudding user response:

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.

  • Related