Under the Windows platform, test the Nodejs connection to the MySQL database.
First install the MySQL dependency package in the console
At first I was in the corresponding JS located in the directory under the installation of MySQL support, the result is:
When I saw so many warn, I thought I had a problem with my outfit, so I was ready for a global installation, NPM install-g MySQL
There is no error and warn.
However, when JS is executed, an exception is thrown
The reason is: After successful installation, the MySQL dependency package is found in the C:\Users\ Chen \appdata\roaming\npm directory, and the JS file does not correspond to the path that is introduced into the MySQL module.
If the JS file is not in the user directory, put JS in the E-disk Nodejs folder, run node Test14_mysql.js will be reported cannot find module ' mysql ' Error
This is because node is executing mysql.js in the E:\NodeJS directory, and node cannot find the MySQL module in the Nodejs directory, so this error is reported.
Workaround:
You can put the JS file in the user directory, and then node test14_mysql.js;
You can also install the MySQL dependency package in the Nodejs directory, and then node test14_mysql.js;
There are two ways to install a dependency package in the Nodejs directory:
1. Direct console into the Nodejs directory, then npm install MySQL
2. Create a Package.json file in the Nodejs directory, add the MySQL dependency, and then execute the NPM install in the Nodejs directory, that is, in fact, I started the installation of the successful, but was warn to scare
node. js cannot find module ' MySQL '