Dotenv: Process cannot find .env variables

Created on 23 Apr 2020  路  10Comments  路  Source: motdotla/dotenv

dotenv is not discovering any env variables in my running processes. Here is an application that I am having trouble with:

require('dotenv').config();

const Koa = require('koa');
const mount = require('koa-mount');
const logger = require('koa-logger');

const api = new Koa();
const paint = require('./apps/paint.js');
const { PORT } = process.env;

api.use(logger());

api.use(mount('/paint', paint));

api.listen(PORT, () => {
  console.log(`Listening at port ${PORT}\nCan be found externally at https://example.com/api`);
});

The application above gives the following response, and does not listen to the set port:

Listening at port undefined
Can be found externally at https://example.com/api

However, when i manually give PORT a value, i.e. 5000, it gives the wanted response, and listens to the port:

Listening at port 5000
Can be found externally at https://example.com/api

Here are the relevant parts of my package.json:

  "dependencies": {
    "@koa/cors": "^3.0.0",
    "@koa/multer": "^2.0.2",
    "@koa/router": "^8.0.8",
    "@tadashi/koa-jwt-authz": "^1.0.0",
    "dotenv": "^8.2.0",
    "jwks-rsa": "^1.8.0",
    "koa": "^2.11.0",
    "koa-body": "^4.1.1",
    "koa-jwt": "^3.6.0",
    "koa-logger": "^3.2.1",
    "koa-mount": "^4.0.0",
    "koa-static": "^5.0.0",
    "multer": "^1.4.2"
  },
  "devDependencies": {
    "eslint": "^6.8.0",
    "eslint-config-koa": "^2.0.2",
    "eslint-config-standard": "^14.1.1",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.1"
  }

All 10 comments

how is your .env file?

My .env file is in the application root directory. Following are the relevant lines from the file:

NODE_ENV=development
PORT=5000
...

Here is the directory tree:

api/
|  apps/
|  |  paint.js
|  node_modules/
|  .env
|  api.js
|  package.json
|  package-lock.json

really... is correct.
could you share with me your repository for me to test here on my machine?

I am getting this same problem.

Error:

src/index.ts:3:23 - error TS2532: Object is possibly 'undefined'.
const port: number = +process.env.PORT || 3333;

@chiq2045 you will want you .env file to be at the root of the process you are running if you want it to automatically parse the file. process.cwd is what we use to find the .env file.

If you need to use the file in your API directory you will need to configure the path the the .env file.

Let me know if this helps.

I'm not sure what has changed over the last few days, but now it is working. I have not made any changes on my end. @joaoliveirajs are things working for you now?

Also, @jcblw, api directory is the root directory, and that is also what process.cwd returns.

@chiq2045

Great! :smile:

Here it works too

image

awesome great to hear! @joaoliveirajs let me know if you are still having issues. Closing for now.

I just wanted to add an addendum. It seems that the problem that I am experiencing is related to #229.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shellscape picture shellscape  路  3Comments

marcusradell picture marcusradell  路  3Comments

shellscape picture shellscape  路  3Comments

awesomejerry picture awesomejerry  路  4Comments

Quocnamit picture Quocnamit  路  3Comments