Node js unexpected token - javascript

i'm trying to run my project on centos 7 OS, i'm trying to install my project by writing node install_server.js
and the terminal returns me an error
install_server.js:26
fs.readFile(`${__dirname}/config/config.sql`, (err, data) => {
^
SyntaxError: Unexpected token ILLEGAL
at Module._compile (module.js:439:25)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
And my source code of this file is found below
const cfg = require('./config/config.json');
const knex = require('knex');
const fs = require('fs');
const mysql = require('mysql');
var db = knex({
client: 'mysql',
pool: {
min: 0,
max: 1
},
connection: {
host: cfg.database.db_server,
user: cfg.database.db_user,
password: cfg.database.db_password
}
});
var myCon = mysql.createConnection({
multipleStatements: true,
host: cfg.database.db_server,
user: cfg.database.db_user,
password: cfg.database.db_password
});
fs.readFile(`${__dirname}/config/config.sql`, (err, data) => {
if (err)
throw err;
myCon.query(data.toString(), function(err, results) {
if (err)
throw err;
console.log('complete')
});
});
Cant seem to figure it out, could i get a helphand?

Template Literals are not supported until version 4.8.4 according to
http://node.green/
which is a great resource.

Related

Getting MODULE_NOT_FOUND error when using require() for a .js file in sub directory

Using ExpressJS, when I run my project using 'node server.js' command, I get the following error:
node:internal/modules/cjs/loader:936
throw err;
^
Error: Cannot find module '../config/db.config.js'
Require stack:
/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/models/event.model.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/controllers/event.controller.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/routes/event.routes.js
/Users/umairkhan/Documents/nodejs/bohra-calendar/server.js
at Function.Module._resolveFilename (node:internal/modules/cjs/loader:933:15)
at Function.Module._load (node:internal/modules/cjs/loader:778:27)
at Module.require (node:internal/modules/cjs/loader:1005:19)
at require (node:internal/modules/cjs/helpers:94:18)
at Object. (/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js:2:16)
at Module._compile (node:internal/modules/cjs/loader:1101:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
at Module.load (node:internal/modules/cjs/loader:981:32)
at Function.Module._load (node:internal/modules/cjs/loader:822:12)
at Module.require (node:internal/modules/cjs/loader:1005:19) {
code: 'MODULE_NOT_FOUND',
requireStack: [
'/Users/umairkhan/Documents/nodejs/bohra-calendar/db.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/models/event.model.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/controllers/event.controller.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/routes/event.routes.js',
'/Users/umairkhan/Documents/nodejs/bohra-calendar/server.js'
]
}
This is my code inside the db.js file:
const mysql = require("mysql");
var dbConfig = require("../config/db.config.js");
// Create a connection to the database
const connection = mysql.createConnection({
host: dbConfig.HOST,
user: dbConfig.USER,
password: dbConfig.PASSWORD,
database: dbConfig.DB
});
// open the MySQL connection
connection.connect(error => {
if (error) throw error;
console.log("Successfully connected to the database.");
});
module.exports = connection;
Here is the directory structure of the project.
I have also tried this path: './config/db.config.js' but I still get the same error.
Can someone please help?

Cant access Azure SQL server from nodejs

I am trying to do a simple query to my azure SQL server from NodeJs, im getting errors that i cannot connect to the server. all the details are correct because i used them on another application i built in C#.
my code:
var mysql = require('mysql');
var config =
{
host: Server,
user: UserName,
password: PassWord,
database: Database
};
const conn = new mysql.createConnection(config);
conn.connect(
function (err) {
if (err) {
console.log("!!! Cannot connect !!! Error:");
throw err;
}
else
{
console.log("Connection established.");
queryDatabase();
}
});
function queryDatabase(){
conn.query('SELECT * FROM Drinks', function (err, results, fields)
{
if (err) throw err;
console.log(results);
})
conn.end(function (err) {
if (err) throw err;
else console.log('Done.')
});
};
I have tried making port rules, allowing nodejs through firewall, turning off firewall, I have added my IP to the firewall on the azure group and still cant connect. Any help would be awesome. Thanks!
Error:
Error: read ECONNRESET
at TCP.onStreamRead (node:internal/stream_base_commons:211:20)
--------------------
at Protocol._enqueue (C:\node\node_modules\mysql\lib\protocol\Protocol.js:144:48)
at Protocol.handshake (C:\node\node_modules\mysql\lib\protocol\Protocol.js:51:23)
at Connection.connect (C:\node\node_modules\mysql\lib\Connection.js:116:18)
at Object.<anonymous> (C:\node\test.js:14:6)
at Module._compile (node:internal/modules/cjs/loader:1108:14)
at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
at Module.load (node:internal/modules/cjs/loader:973:32)
at Function.Module._load (node:internal/modules/cjs/loader:813:14)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:76:12)
at node:internal/main/run_main_module:17:47 {
errno: -4077,
code: 'ECONNRESET',
syscall: 'read',
fatal: true
}
Ok so i worked out how to do it! and for anyone with similar issues do this.
Instead of mysql i used mssql and it worked straight away.
Example:
var sql = require('mssql');
var config = {
server: "XXX.database.windows.net",
database: "XXX",
user: "XXX",
password: "XXX",
port: 1433,
options: {
encrypt: true
}
};
sql.connect(config).then(function() {
console.log("connected")
sql.query('SELECT * FROM Drinks', function (err, results, fields)
{
if (err) throw err;
console.log(results);
})
})

MariaDB connection with Sequelize

I have been checking for the connectivity of MariaDB, with Sequelize.
const Sequelize = require('sequelize');
// Setting up database (MariaDB) connection
const sequelize = new Sequelize('dbName', 'usr', 'pass', {
host: 'localhost',
dialect: 'mariadb'
});
But I am getting the following error:
/home/lt-196/api/node_modules/sequelize/lib/sequelize.js:236
throw new Error('The dialect ' + this.getDialect() + ' is not supported. Supported dialects: mssql, mysql, postgres, and sqlite.');
^
Error: The dialect mariadb is not supported. Supported dialects: mssql, mysql, postgres, and sqlite.
at new Sequelize (/home/lt-196/api/node_modules/sequelize/lib/sequelize.js:236:15)
at Object.<anonymous> (/home/lt-196/api/app.js:21:19)
at Module._compile (module.js:652:30)
at Object.Module._extensions..js (module.js:663:10)
at Module.load (module.js:565:32)
at tryModuleLoad (module.js:505:12)
at Function.Module._load (module.js:497:3)
at Function.Module.runMain (module.js:693:10)
at startup (bootstrap_node.js:191:16)
at bootstrap_node.js:612:3
MariaDB
For MariaDB compatibility you have to install the package mariasql#0.1.20, or higher.
The configuration needs to look like this:
var sequelize = new Sequelize('database', 'username', 'password', {
dialect: 'mariadb'
})
Or Try this:
MariaSQL: https://www.npmjs.com/package/mariasql
A node.js binding to MariaDB's non-blocking (MySQL-compatible) client library.
var Client = require('mariasql');
var c = new Client({
host: '127.0.0.1',
user: 'foo',
password: 'bar'
});
c.query('SHOW DATABASES', function(err, rows) {
if (err)
throw err;
console.dir(rows);
});
c.end();
MariaSQL is recommended.
https://github.com/MariaDB/mariadb-connector-nodejs
NPM
npm install --save mariadb
npm install --save sequelize#next
Yarn
yarn add mariadb
yarn add sequelize#next
const Sequelize = require('sequelize'),
sequelize = new Sequelize(process.env.db_name, process.env.db_user, process.env.db_pass, {
dialect: 'mariadb',
dialectOptions: {
socketPath: process.env.db_socket,
timezone: process.env.db_timezone
},
pool: {
min: 0,
max: 5,
idle: 10000
},
define: {
charset: 'utf8',
timestamps: false
},
benchmark: false,
logging: false
})

error while creating database using node js

I am new to node js and i am trying to create a database.
i copied code from w3school and tried to run in my pc.
i got the following error:
C:\Users\HP\Desktop>node db.js
C:\Users\HP\Desktop\db.js:10
if (err) throw err;
^
Error: connect ECONNREFUSED 127.0.0.1:3306
at Object._errnoException (util.js:999:13)
at _exceptionWithHostPort (util.js:1020:20)
at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1207:14)
--------------------
at Protocol._enqueue (C:\Users\HP\node_modules\mysql\lib\protocol\Protocol.j
s:145:48)
at Protocol.handshake (C:\Users\HP\node_modules\mysql\lib\protocol\Protocol.
js:52:23)
at Connection.connect (C:\Users\HP\node_modules\mysql\lib\Connection.js:130:
18)
at Object.<anonymous> (C:\Users\HP\Desktop\db.js:9:5)
at Module._compile (module.js:660:30)
at Object.Module._extensions..js (module.js:671:10)
at Module.load (module.js:573:32)
at tryModuleLoad (module.js:513:12)
at Function.Module._load (module.js:505:3)
at Function.Module.runMain (module.js:701:10)
C:\Users\HP\Desktop>
db.js:
var mysql = require('mysql');
var con = mysql.createConnection({
host: "localhost",
user: "myusername",
password: "mypassword"
});
con.connect(function(err) {
if (err) throw err;
console.log("Connected!");
/*Create a database named "mydb":*/
con.query("CREATE DATABASE mydb", function (err, result) {
if (err) throw err;
console.log("Database created");
});
});
I am completely new to node js.

Runner is undefined in query of Knex.js

I tried to use Knex.js on Node.js by following code:
var Promise = require("bluebird");
var knex = require("knex")({
client: 'pg',
user : 'username',
database : 'database',
password: "password",
migrations: {
tableName: 'knex_migrations'
},
pool: {
min: 0,
max: 7
}
});
knex.select("*").from("users").then(function(rows){
console.log(rows);
});
But, it threw an error about Runner object as follows:
/path/to/myapp/node_modules/knex/lib/interface.js:27
return new Runner(this).run().then(onFulfilled, onRejected);
^
TypeError: undefined is not a function
at QueryBuilder_PG.Target.then (/path/to/myapp/node_modules/knex/lib/interface.js:27:12)
at Object.<anonymous> (/path/to/myapp/test.js:14:32)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
When I try to use stream, pipe, exec and transaction, same error occurred.
Please tell me how to solve this problem...
I was really careless about configuration of client.
I have mistaken in description of "connection". It have to be written as follows:
var Promise = require("bluebird");
var knex = require("knex")({
client: 'pg',
connection: {
host : '127.0.0.1',
user: 'username',
database: 'database',
password: 'password'
},
migrations:{
tableName:"knex_migrations"
},
pool: {
min: 0,
max: 7
}
});
knex.select("*").from("users").then(function(rows){
console.log(rows);
});
It works with no problems.
Thank you!

Categories

Resources