While debugging it is showing same error, but without debugging it is working .
I tried all solutions which i saw but still it isn’t working. I need some suggestions.
The below code is in index.js
//db connection
dotenv.config();
const connection = require("./models/index");
connecton code is present in models
const mysql = require('mysql2')
const connection = mysql.createConnection({
host: process.env.DB_HOST,
user: process.env.DB_USER,
password: process.env.DB_PASSWORD,
database: process.env.DB_DATABASE,
port: process.env.DB_PORT,
multipleStatements: true
});
module.exports = connection
Error is showing like this below
server is running on 8011
index.js:37
Process exited with code 1
Uncaught Error Error: Access denied for user ''@'localhost' (using password: NO)
at asError (file:///home/rohitchitampalle/auth_process/backend_auth/node_modules/mysql2/lib/packets/packet.js:728:17)
at execute (file:///home/rohitchitampalle/auth_process/backend_auth/node_modules/mysql2/lib/commands/command.js:29:26)
at handlePacket (file:///home/rohitchitampalle/auth_process/backend_auth/node_modules/mysql2/lib/connection.js:481:34)
at <anonymous> (file:///home/rohitchitampalle/auth_process/backend_auth/node_modules/mysql2/lib/connection.js:97:12)
at executeStart (file:///home/rohitchitampalle/auth_process/backend_auth/node_modules/mysql2/lib/packet_parser.js:75:16)
at <anonymous> (file:///home/rohitchitampalle/auth_process/backend_auth/node_modules/mysql2/lib/connection.js:104:25)
at emit (node:events:514:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Readable.push (node:internal/streams/readable:234:10)
at onStreamRead (node:internal/stream_base_commons:190:23)
at callbackTrampoline (node:internal/async_hooks:130:17)
--- TCPWRAP ---
at init (node:internal/inspector_async_hook:25:19)
at emitInitNative (node:internal/async_hooks:202:43)
at Socket.connect (node:net:1176:7)
at connect (node:net:244:17)
at Connection (file:///home/rohitchitampalle/auth_process/backend_auth/node_modules/mysql2/lib/connection.js:50:27)
at exports.createConnection (file:///home/rohitchitampalle/auth_process/backend_auth/node_modules/mysql2/index.js:10:10)
at <anonymous> (file:///home/rohitchitampalle/auth_process/backend_auth/src/models/index.js:4:26)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at Module.require (node:internal/modules/cjs/loader:1143:19)
at require (node:internal/modules/cjs/helpers:121:18)
at <anonymous> (file:///home/rohitchitampalle/auth_process/backend_auth/src/index.js:10:20)
at Module._compile (node:internal/modules/cjs/loader:1256:14)
at Module._extensions..js (node:internal/modules/cjs/loader:1310:10)
at Module.load (node:internal/modules/cjs/loader:1119:32)
at Module._load (node:internal/modules/cjs/loader:960:12)
at executeUserEntryPoint (node:internal/modules/run_main:81:12)
at <anonymous> (node:internal/main/run_main_module:23:47)
2
Answers
The error is because the environment variables is not loaded/populated. You can log the values of the process.env to confirm this.
Import and configure dotenv as early as possible, it must be executed first otherwise you will need to keep on configuring it all the time in all your files. See the documentation: https://www.npmjs.com/package/dotenv#%EF%B8%8F-usage.
In your case, you need to make sure that index.js is loaded first and you should define it properly.
OR
I think this is because you have not imported .env file, try the below solution.
1.Install dotenv using npm or yarn add dotenv
2.import .env using require(‘dotenv’).config();