0

I am new to node.js and ran into the following problem. I run my node.js app using

cd bin
node www.js

and it works fine.

And in my package.json I added the following section

 "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start": "node bin/www.js"
  },

now when I type

npm start

in the terminal(I am using vscode)

my project runs normally.

But when I go to a page where some data should be retrieved from the database I get

: Access denied for user 'root'@'localhost'

error. When I do the same using node www.js everythings is ok, data is retrieved from db, and there is no prblem.

So what actually does npm start, I think it should do the same as node www.js but in fact there is some differences.

what can be the reason of such kind of behaviour?

Karen Avdalyan
  • 382
  • 1
  • 20

1 Answers1

0

I've found the problem in my code, actually absolutely different problem caused access denied. The problem was in not being able to read .env file.

My .env file is in bin/ folder and in the same folder I run my project

node bin/www.js

I found here that the .env must be in the root folder, from where the application starts so because of it

npm start

didn't work as I understand it doesn't run the application directly from bin\www.js So changing

require('dotenv').config();

to

require('dotenv').config({path: __dirname + '/.env'});

solve my problem

Karen Avdalyan
  • 382
  • 1
  • 20