How do I setup the dotenv file in Node.js?

前端 未结 30 1139

I am trying to use the dotenv NPM package and it is not working for me. I have a file config/config.js with the following content:



        
相关标签:
30条回答
  • 2020-12-07 14:46

    If you use "firebase-functions" to host your sever-side-rendered application, you should be aware of this one:

    error: Error: ENOENT: no such file or directory, open 'C:\Codes\url_shortener\functions\.env'

    Means you have to store the .env file in the functions folder as well.

    Found this one by:

    console.log(require('dotenv').config())
    
    0 讨论(0)
  • 2020-12-07 14:47

    In my case, every time I tried to get a key from the .env file using process.env.MY_KEY, it returned undefined.

    I suffered from this problem for two hours just because I named the file something like keys.env which is not considered to be a .env file.

    So here is the troubleshooting list:

    1. The filename should be .env (I believe .env.test is also acceptable).

    2. Make sure you are requiring it as early as possible in your application using this statement require('dotenv').config();

    3. The .env file should be in the root directory of your project.

    4. Follow the "file writing rules" like DB_HOST=localhost, no need to wrap values in double/single quotes.

    Also, check the documentation of the package on the NPM site.

    0 讨论(0)
  • 2020-12-07 14:48

    Had the same issue recently. Check your .env file and use equal sign not colon. Here's an example:

    key=value
    

    instead of:

    key:value
    
    0 讨论(0)
  • 2020-12-07 14:50

    i didn't put my environment variables in the right format as was in the dotenv module documentation e.g. i was doing export TWILIO_CALLER_ID="+wwehehe" and so the dotenv module wasn't parsing my file correctly. When i noticed that i removed the export keyword from the declarations and everything worked fine.

    0 讨论(0)
  • 2020-12-07 14:53

    Save yourself some troubleshooting time and log your require call, like so:

    console.log(require('dotenv').config())
    

    You should see an error with more detailed info on the problem.

    0 讨论(0)
  • 2020-12-07 14:54

    My code structure using is as shown below

    -.env
    -app.js
    -build
    -src
       |-modules
            |-users
                |-controller
                      |-userController.js
    

    I have required .env at the top of my app.js

    require('dotenv').config();
    import express = require('express');
    import bodyParser from 'body-parser';
    import mongoose = require('mongoose');
    

    The process.env.PORT works in my app.listen function. However, on my userController file not sure how this is happening but my problem was I was getting the secretKey value and type as string when I checked using console.log() but getting undefined when trying it on jwt.sign() e.g.

    console.log('Type: '+ process.env.ACCESS_TOKEN_SECRET)
    console.log(process.env.ACCESS_TOKEN_SECRET)
    

    Result:

    string
    secret
    

    jwt.sign giving error

    let accessToken = jwt.sign(userObj, process.env.ACCESS_TOKEN_SECRET); //not working 
    

    Error was

        Argument of type 'string | undefined' is not assignable to parameter of type 'Secret'.
      Type 'undefined' is not assignable to type 'Secret'.
    

    My Solution: After reading the documentation. I required the env again in my file( which I probably should have in the first place ) and saved it to variable 'environment'

    let environment = require('dotenv').config();
    

    console logging environment this gives:

    {
    parsed: {
        DB_HOST: 'localhost',
        DB_USER: 'root',
        DB_PASS: 'pass',
        PORT: '3000',
        ACCESS_TOKEN_SECRET: 'secretKey',
      }
    }
    

    Using it on jwt.sign not works

    let accessToken = jwt.sign(userObj, environment.parsed.ACCESS_TOKEN_SECRET);
    

    Hope this helps, I was stuck on it for hours. Please feel free to add anything to my answer which may help explain more on this.

    0 讨论(0)
提交回复
热议问题