The sync-sql module is designed to make synchronous queries to the database. Since normal SQL queries are asynchronous in node.js but if you want to run it synchronously, then it is possible using this module. In synchronized SQL, the result set is returned when the query is executed.
Note: Do not use this module in production mode as node.js is designed to be asynchronous.
Installation of sync-sql module:
- You can visit the link Install sync-sql module. You can install this package by using this command.
npm install sync-sql
- After installing sync-sql you can check your sync-sql version in command prompt using the command.
npm version sync-sql
- After that, you can just create a folder and add a file for example index.js, To run this file you need to run the following command.
node index.js
- Now open MySQL and create a database for example ‘demo’.
Filename: index.js
const Mysql = require('sync-mysql') const connection = new Mysql({ host:'localhost', user:'root', password:'password', database:'demo'}) var result = connection.query('SELECT NOW()') console.log(result) |
Steps to run the program:
- The project structure will look like this:

- Make sure you install sync-sql using following commands:
npm install sync-sql
- Run index.js file using below command:
node index.js

So this is how you can run synchronized SQL queries in node js using sync-sql package.
Recommended Posts:
- Nodejs | Automatic restart NodeJs server with nodemon
- Synchronous and Asynchronous in JavaScript
- Run Python script from Node.js using child process spawn() method
- Create a web server and run PHP script on it using Raspberry Pi
- Run Python Script using PythonShell from Node.js
- Nodejs - Connect MongoDB with Node app using MongooseJS
- Cowsay in Nodejs using Requests library
- Nodejs | Web Crawling using Cheerio
- Signup Form Using Nodejs and MongoDB
- Nodejs | authentication using Passportjs and passport-local-mongoose
- NodeJs CRUD Operations Using Mongoose and MongoDB Atlas
- Generating Lighthouse Scores using NodeJS
- How to run JavaScript from PHP?
- How do you run JavaScript script through the Terminal?
- How to run a function when the page is loaded in JavaScript ?
- How to run Cron Jobs in Node.js ?
- How to run two animations simultaneously in jQuery ?
- How to run node.js program as an Executable ?
- Docker compose tool to run multi container applications
- How to Make, Save, and Run a Simple VBScript Program?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

