feathers icon indicating copy to clipboard operation
feathers copied to clipboard

Using dotenv doesn't work with.

Open JPStrydom opened this issue 5 months ago • 2 comments

When following the dotenv documentation, I cannot get dotenv to load my environment variables correctly.

What I've done:

  1. Created a brand new Feather.js app
  2. Installed dotenv as per the documentation
  3. Added a .env file to both the root folder and the src folder, these just has a MEMES=MEMES-WORKS variable in it
  4. Made a test required configuration in configuration.js like this:
    export const configurationSchema = Type.Intersect([
      defaultAppConfiguration,
      Type.Object({
        host: Type.String(),
        port: Type.Number(),
        public: Type.String(),
        memes: Type.String() // Made this required to test
      })
    ])
    
  5. Updated my config/custom-environment-variables.json file like this:
    {
      "port": {
        "__name": "PORT",
        "__format": "number"
      },
      "host": "HOSTNAME",
      "authentication": {
        "secret": "FEATHERS_SECRET"
      },
      "memes": "MEMES"
    }
    
  6. Ran the following at the top of my app.js and index.js file:
    import * as dotenv from 'dotenv'
    
    dotenv.config();
    

When I start the API, I keep getting this error:

error: Unhandled Rejection ValidationError: validation failed
at validate10 (eval at compileSchema (directory\feathers-dotenv-issue\node_modules\ajv\dist\compile\index.js:89:30), <anonymous>:3:529)
at app.hooks.setup (directory\feathers-dotenv-issue\node_modules\@feathersjs\configuration\lib\index.js:25:31)
at Feathers.dispatch (directory\feathers-dotenv-issue\node_modules\@feathersjs\hooks\script\compose.js:30:43)
at Feathers.hookChain (directory\feathers-dotenv-issue\node_modules\@feathersjs\hooks\script\hooks.js:27:28)
at Feathers.dispatch (directory\feathers-dotenv-issue\node_modules\@feathersjs\hooks\script\compose.js:30:43)
at Feathers.<anonymous> (directory\feathers-dotenv-issue\node_modules\@feathersjs\hooks\script\compose.js:16:25)
at Feathers._setup (directory\feathers-dotenv-issue\node_modules\@feathersjs\hooks\script\hooks.js:44:53)
at Feathers.listen (directory\feathers-dotenv-issue\node_modules\@feathersjs\koa\lib\index.js:58:24)
at file:///C:/Data/Development/MTN/Playgrounds/feathers-dotenv-issue/src/index.js:13:5
at ModuleJob.run (node:internal/modules/esm/module_job:274:25) {
  errors: [
    {
      instancePath: '',
      schemaPath: '#/required',
      keyword: 'required',
      params: [Object],
      message: "must have required property 'memes'"
    }
  ],
  validation: true,
  ajv: true
}

I've also tried updating my config/default.json file like so:

{
  "host": "localhost",
  "port": 3030,
  "public": "./public/",
  "origins": [
    "http://localhost:3030"
  ],
  "paginate": {
    "default": 10,
    "max": 50
  },
  "memes": "MEMES_DOES_NOT_WORK"
}

But then app.get('memes') keeps on returning "MEMES_DOES_NOT_WORK".

Changing from:

import * as dotenv from 'dotenv'

dotenv.config();

to

import 'dotenv/config';

does seem to work, but the problem is that I have a .env file that I need to read from a parent monorepo directory. So my actual file needs to look like this:

import * as dotenv from 'dotenv';

dotenv.config({ path: ['../../../.env.development.local', '../../../.env.development'] });

Is there a way to specify the import path when using the import 'dotenv/config'; syntax?

Here is a repo that reproduces the issue: feathers-dotenv-issue.zip

JPStrydom avatar May 21 '25 15:05 JPStrydom