How to use nodemon with .env files?

后端 未结 11 934
伪装坚强ぢ
伪装坚强ぢ 2020-12-09 02:37

I am using an .env file to hold environment variables for the server. This works if I run the server with foreman start. But it doesn\'t work with nodemon.

I would

相关标签:
11条回答
  • 2020-12-09 02:38

    In Three steps

    1. Creating the file on root folder > .env
    # .env ======
    PORT=5000
    WHO_AM_I="Who Knows"
    
    1. Install the dotenv
    2. Run below command
    "dev": "nodemon -r dotenv/config src/app.js"
    

    You can access the your defined variables using > process.env.varible_name

    0 讨论(0)
  • 2020-12-09 02:38

    Use the -w key to specify nodemon what to watch additionally.

    "scripts": {
        "dev": "env-cmd nodemon -w app -w *.js -w .env server.js"
    }
    

    Don't forget rerun npm run dev

    0 讨论(0)
  • 2020-12-09 02:39

    Thread necromancy!

    Use grunt-env to load environmental variables from your heroku config.

    0 讨论(0)
  • 2020-12-09 02:40

    Place your local configuration variables in the .env file and run foreman along with nodemon using the following command

    $ foreman run nodemon web.js
    
    0 讨论(0)
  • 2020-12-09 02:41

    Heroku Procfile

    Change: web: node app.js to web: nodemon app.js

    0 讨论(0)
  • 2020-12-09 02:53

    To load the dotenv package and any declared .env vars into the environment, you can do the following:

    nodemon -r dotenv/config myapp.js
    
    0 讨论(0)
提交回复
热议问题