I created an api to retrieve data in the database I created but it can’t connect.
MY CODE
`const express = require("express");`
`const mysql = require("mysql");`
`const app = express();`
`const database = mysql.createConnection({`
` host: "localhost",`
` user: "root",`
` password: "",`
` database: "tes_api",`
`});`
`database.connect((err) => {`
` if (err) throw err;`
` console.log("Database Connected!");`
`});`
`app.get("/",(req,res) => {`
` res.send("Hello World");`
`});`
`app.listen (3030,()=>{`
` console.log("Server is running on port 3030");`
`});`
enter image description here
OUTPUT IN TERMINAL
PS C:Usersdigittes_api> npm run dev
> [email protected] dev
> nodemon index.js
[nodemon] 3.1.0
[nodemon] to restart at any time, enter `rs`
[nodemon] watching path(s): *.*
[nodemon] watching extensions: js,mjs,cjs,json
[nodemon] starting `node index.js`
node:internal/modules/cjs/loader:1146
throw err;
^
Error: Cannot find module 'mysql'
Require stack:
- C:Usersdigittes_apiindex.js
at Module._resolveFilename (node:internal/modules/cjs/loader:1143:15)
at Module._load (node:internal/modules/cjs/loader:984:27)
at Module.require (node:internal/modules/cjs/loader:1234:19)
at require (node:internal/modules/helpers:176:18)
at Object.<anonymous> (C:Usersdigittes_apiindex.js:2:15)
at Module._compile (node:internal/modules/cjs/loader:1375:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1434:10)
at Module.load (node:internal/modules/cjs/loader:1206:32)
at Module._load (node:internal/modules/cjs/loader:1022:12)
at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:142:12) {
code: 'MODULE_NOT_FOUND',
requireStack: [ 'C:\Users\digit\tes_api\index.js' ]
}
Node.js v21.5.0
[nodemon] app crashed - waiting for file changes before starting...
enter image description here
MY DATABASE
enter image description here
I have tried to check the node_module but there is no problem there and I use nodemon so that I don’t restart my server if there are changes in my code. Is the problem in the nodemon?
New contributor
Muhamad Nur Arif is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1