Reading enviromental variable problem in node.js

Here is my excel.js:

    let test = async () => {
         console.log(process.env.DATABASE_HOST);
         .......
    }
    test();

Here is my package.json fragment:

"scripts": {
    .............
    "excel": "cross-env NODE_ENV=development node ./server/excel.js",
    "test": "react-scripts test"
  }

My .env.development is stored in the application root folder.
Here is my .env.development:

DATABASE_HOST=dbServer

When I execute the following command line in the application root folder:

npm run excel

It should return "dbServer", unfortunately, it returns undefined.
How can I fix it?

>Solution :

Install dotenv package, and require it require('dotenv').config()

Leave a Reply