Syntax error in Node module : Sql2: unexpected token

794 Views Asked by At

I am writing this basic code to connect to sql2:

const mysql = require('mysql2');

var con = mysql.createConnection({
    host: "localhost",
    user: "abc",
    password: "123",
    database: "test"
});

con.connect(function (err) {
    if (err) throw err;
    console.log("Connected!");

});

it gives the following error:

/home/mihir/Node_api/node_modules/lru-cache/dist/cjs/index.js:359
    #initializeTTLTracking() {
                          ^

SyntaxError: Unexpected token '('
    at wrapSafe (internal/modules/cjs/loader.js:915:16)
    at Module._compile (internal/modules/cjs/loader.js:963:27)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)
    at Module.load (internal/modules/cjs/loader.js:863:32)
    at Function.Module._load (internal/modules/cjs/loader.js:708:14)
    at Module.require (internal/modules/cjs/loader.js:887:19)
    at require (internal/modules/cjs/helpers.js:74:18)
    at Object.<anonymous> (/home/mihir/Node_api/node_modules/lru-cache/dist/cjs/index-cjs.js:5:36)
    at Module._compile (internal/modules/cjs/loader.js:999:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)

What am I doing wrong?

I first tried using normal sql module, but that starting giving errors so I used sql2, after I got this error, I tried checking this module file and the syntax seems to be Javascript, and I don't want to mess with modules. I tried to find something online but couldn't find anything reliable.

I am stuck and dont know what to do. any help will be appriciated

1

There are 1 best solutions below

0
On

See this related post. Though the mentioned minimal node version is 12.0.0, I can verify I used v12.22.7 and it did not work. Using v18.16.1 solved the issue for me