-1

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?

  • 2
    of course it will not work. You're trying to require non js file which is binary file for mysql client. just for fun do: `sudo cat /usr/local/bin/mysql` if You'll see js source code as output come and hit me :) – num8er Oct 04 '21 at 23:04
  • 1
    if You want to connect to mysql use mysql npm package and try examples from it's page: https://www.npmjs.com/package/mysql – num8er Oct 04 '21 at 23:06
  • in those examples, they use require('mysql'), which I tried and it said "module not found," even though I've installed it. That's why I tried listing out the full path. Know why it would say "module not found"? – JamesDockett Oct 04 '21 at 23:18
  • And actually if I set require() equal to a variable, I get: var = require('mysql') SyntaxError: Unexpected token '='... I'm very confused – JamesDockett Oct 04 '21 at 23:20
  • please watch nodejs tutorials before guessing what and how to do, cause You're failing on basics of node and packaging – num8er Oct 04 '21 at 23:22
  • 2
    please watch at least these tutorials: https://www.youtube.com/watch?v=fBNz5xF-Kx4, https://www.youtube.com/watch?v=L72fhGm1tfE, https://www.youtube.com/watch?v=EN6Dx22cPRI – num8er Oct 04 '21 at 23:24
  • 1
    also use VS Code or WebStorm which will point to Your mistakes – num8er Oct 04 '21 at 23:26
  • 2
    MySQL (the DB) and the NodeJS [mysql](https://www.npmjs.com/package/mysql) package are entirely different things. The first is a DB server and the latter is a client library for JavaScript applications you use to connect to and interact with the former – Phil Oct 04 '21 at 23:47

2 Answers2

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);
});
num8er
  • 18,604
  • 3
  • 43
  • 57
-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.