How to Install MySQL Database Package or Module for Web-based Application powered by Express Framework in Node.js

Posted on

This article is an article where the title itself is quite a long title. But the main subject is focusing on how to install MySQL Database package or module. The package or module itself is used by the web-based application powered by the express framework. It can be referred from the article titled ‘How to Create an Express Web-based Framework Application to be deployed in Node.js’ in this link. So, the following are steps taken in order for the MySQL Database package or module to be installed in the web-based application :

1. Execute the following command to start installing mysql using npm command tool :

npm install mysql

Below is the actual execution of the command :

user@hostname:~/nodejs/test$ npm install --save mysql
application-name@0.0.1 /home/user/nodejs/test
└─┬ mysql@2.15.0 
  ├── bignumber.js@4.0.4 
  ├─┬ readable-stream@2.3.3 
  │ ├── core-util-is@1.0.2 
  │ ├── isarray@1.0.0 
  │ ├── process-nextick-args@1.0.7 
  │ ├── string_decoder@1.0.3 
  │ └── util-deprecate@1.0.2 
  └── sqlstring@2.3.0 

user@hostname:~/nodejs/test$ 

2. As shown in the above output command tool, there are no errors message generated. So, after executing the command above, check the package.json file whether the name of the package or module which is ‘mysql’ is declared in it. It is a mechanism to check whether the insertion process for the ‘mysql’ package or module is working or not. And definitely, it is already inserted in the file as shown below :

{
  "name": "mycrud",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "start": "nodejs ./bin/www",
    "dev": "nodemon server.js"
  },
  "dependencies": {
    "body-parser": "^1.0.2",
    "cookie-parser": "~1.0.1",
    "debug": "~0.7.4",
    "express": "^4.16.3",
    "jade": "~1.3.0",
    "morgan": "~1.0.0",
    "mysql": "^2.15.0",
    "static-favicon": "~1.0.0"
  },
  "main": "app.js",
  "author": "",
  "license": "ISC",
  "description": "",
  "devDependencies": {
    "grunt": "^1.0.2",
    "grunt-contrib-watch": "^1.1.0",
    "grunt-execute": "^0.2.2",
    "nodemon": "^1.17.5"
  }
}

3. The next step is to make sure that the package or module which in this context, it is installed correctly. And it can be verified by executing the following command :

npm list

The output :

user@hostname:~/nodejs/apps$ npm list | grep mysql
├─┬ mysql@2.15.0
...
user@hostname:~/nodejs/apps$

As shown in the above output of the command executed, mysql package or module has successfully registered through the installation that has been carried out before. So, the package or module is ready and it has been prepared to be used further in the web-based application.

Leave a Reply