How to Use Environment Files (.env) in Node.js - Tutorial (dotenv)

Поділитися
Вставка
  • Опубліковано 3 жов 2024
  • In today's video I'll take you through using .env files in NodeJS - this is done using the popular "dotenv" package.
    .env files are used to store environment-specific configuration or settings for your application so it can easily be ran on various environments. Things like database credentials, API keys or URLs for 3rd party resources can be stored in them.
    For your reference, check this out:
    www.npmjs.com/...
    🏫 My Udemy Courses - www.udemy.com/...
    🎨 Download my VS Code theme - marketplace.vi...
    💜 Join my Discord Server - / discord
    🐦 Find me on Twitter - / dcodeyt
    💸 Support me on Patreon - / dcode
    📰 Follow me on DEV Community - dev.to/dcodeyt
    📹 Join this channel to get access to perks - / @dcode-software
    If this video helped you out and you'd like to see more, make sure to leave a like and subscribe to dcode!
    #dcode #javascript

КОМЕНТАРІ • 73

  • @davidlafontant2185
    @davidlafontant2185 Рік тому +34

    Great video!
    For those working with ES6 and who could face some trouble, you could use this :
    `import dotenv from 'dotenv';
    dotenv.config();`
    instead of require().

  • @edutech8909
    @edutech8909 Рік тому +3

    Quick and straight to the point! thanks man. You just got a new subscriber!

  • @AMG-cc7pi
    @AMG-cc7pi Рік тому

    thanks for the video , just on point. Before watching this video I had to read multiple useless blogs that were long and still were not clear enough and incomplete.

  • @SuperYtc1
    @SuperYtc1 2 роки тому +7

    2:27 is when the vid starts

  • @GuilhermeSiebert
    @GuilhermeSiebert Рік тому +1

    Thanks man. You quickly solved what 5 other videos didn't!

  • @deshangunatunga4175
    @deshangunatunga4175 Рік тому +3

    Great video, short and to the point. Thank you

    • @DAULISG
      @DAULISG Рік тому

      I really appreciate these vids too, "short and to the point"

  • @JuanRomero-cq3dy
    @JuanRomero-cq3dy 2 роки тому +1

    simple and straight forward thanks bro

  • @vasilymedvedev3077
    @vasilymedvedev3077 7 місяців тому

    great video and very consistent explanation, thank you, bro!

  • @peanutcelery
    @peanutcelery Рік тому +8

    If your .env is named different like .env.local then you pass the options in config:
    config({path: "./.env.local"})

  • @GiuseppeGreco-jl7dx
    @GiuseppeGreco-jl7dx Рік тому +1

    Quick and easy. Thanks a lot!

  • @danielfan9196
    @danielfan9196 11 місяців тому

    I LOVE YOU SO MUCH EVERY OTHER VIDEO DIDN'T WORK I LOVE YOU

  • @kennethsolomon2178
    @kennethsolomon2178 2 роки тому

    Thank you so much ! Created my first .env file LOL.

  • @yidan_wang_ittan
    @yidan_wang_ittan 9 місяців тому

    Thank you so much! Help me clear my doubt!!!

  • @anounTT
    @anounTT Рік тому +7

    FYI: The .env file has to be where you start the application. I had index in /src/index.js, and env '/' and my start script was 'cd src && nodex index.js' and it did not detect my env variables. In the video you said in the root, but not all applications start from the root.

    • @adaninz4340
      @adaninz4340 Рік тому +3

      I had the same probleme but I put the .env file in src and requiere dotenv module like this: require(“dotenv”).config({path:”src/.env”});

    • @aseo9524
      @aseo9524 10 місяців тому

      Was having the same issue, your comment helped me. Thanks!

  • @XDTrae
    @XDTrae Рік тому

    Actually a great video. Thank you very much

  • @Eliastaye6
    @Eliastaye6 Рік тому

    Thank you my brother that was helpful

  • @sagigalian9347
    @sagigalian9347 2 роки тому

    thank you my friend, helped me a lot !!!

  • @InshuMussu
    @InshuMussu Рік тому

    Well Explained, Thanks

  • @patricetekeda
    @patricetekeda Рік тому

    Great vid as always

  • @Новаинжиниринг
    @Новаинжиниринг 2 роки тому

    Thank you, it's useful!

  • @b391i
    @b391i 2 роки тому

    Keep Going My Friend On The Internet 😁

  • @jorims8537
    @jorims8537 Рік тому +3

    Why do I need the dotenv dependency though? Can't I just add properties to the process.env in a seperate file and import them to the other sheets ?

  • @patrick_kabwe
    @patrick_kabwe 2 роки тому

    Thankful for the content you are giving out.
    Kindly create a video on how to upload a image or other files with node with a package

  • @gilbertrosales967
    @gilbertrosales967 Рік тому

    Works. Thank you

  • @AlphaSynapse
    @AlphaSynapse 8 місяців тому

    That was helpful 👍

  • @SudhanshuKumar-dg2mj
    @SudhanshuKumar-dg2mj Рік тому

    Great and Thanks Bro 👍👍👍

  • @anandshete9170
    @anandshete9170 6 місяців тому

    You are a GOD!!!!!!!!!!!!

  • @umbertocrema6249
    @umbertocrema6249 Рік тому

    Thanks man!

  • @TheVicj92
    @TheVicj92 10 місяців тому

    Thanks!

  • @JD-hq1kn
    @JD-hq1kn 2 роки тому

    Short Sweet and Effective.

  • @sagyblecher9258
    @sagyblecher9258 Рік тому

    thank you so much!

  • @SaintHanappi
    @SaintHanappi 2 роки тому

    Klick (=like) first, then watch the content ;)))
    Excellent content/topic

  • @clintonboamah6029
    @clintonboamah6029 Рік тому

    Thank you!

  • @adeshkumarpatra2620
    @adeshkumarpatra2620 7 місяців тому

    Thanks!!

  • @vinzegonzalesgamboa1908
    @vinzegonzalesgamboa1908 Рік тому

    nice! ty

  • @developmenthimanshu7324
    @developmenthimanshu7324 Рік тому

    thanks budy

  • @AriKhuu
    @AriKhuu Рік тому

    thank you

  • @falcongold2024
    @falcongold2024 9 днів тому

    if you're ever having issues with .env. for example if it is saying something like needs to be a string...make sure your .env file is in the root of your project

  • @mochalo4912
    @mochalo4912 Рік тому

    thnx !

  • @DmitryLubenskiy
    @DmitryLubenskiy Рік тому

    And how do I setup env in production after deployment?..

  • @grantdoyle1259
    @grantdoyle1259 Рік тому +1

    trying to use a env file in a vite project but getting this: Uncaught ReferenceError: require is not defined

    • @codeisaurabh
      @codeisaurabh Рік тому

      It looks like you have not installed packages

  • @abcd1235911
    @abcd1235911 Рік тому

    How do we get the cloud foundry env variables like VCAP_APPLICATION in node.js ?

  • @aaronargottelopez3488
    @aaronargottelopez3488 Рік тому

    Thank you! This was awesome. Notheless, could you set subtitles in your next videos, please?

  • @dph6539
    @dph6539 2 роки тому

    I have a problem and also a question. what if I want to use multiple databases in dotenv?

  • @yashsrivastav3859
    @yashsrivastav3859 Рік тому

    thanks

  • @mboe94
    @mboe94 Рік тому +1

    Mine says "undefined"
    Any ideas? Been looking around for 5 hours now. lol. programming is fun.

  • @999hibuki
    @999hibuki Рік тому

    ty

  • @qwerte
    @qwerte 6 місяців тому

    Starts at 2:28

  • @oliverwolfson
    @oliverwolfson 2 роки тому

    worked

  • @DawitMengistuAbajifar
    @DawitMengistuAbajifar Рік тому

    .env file should be outside of any folder like routes or any other
    that solved my undefined problem.

    • @crazyboyrish2577
      @crazyboyrish2577 7 місяців тому

      Thanks alot brother you saved my day❤

    • @twizzy5083
      @twizzy5083 4 місяці тому

      mine is outside of any folder and i still get undefined :(

  • @harshilpatel3389
    @harshilpatel3389 Рік тому

    which theme are you using?

    • @dcode-software
      @dcode-software  Рік тому

      The theme I'm using is called "dcode" and I made it for my UA-cam videos. You can find it on VS Code marketplace

  • @twizzy5083
    @twizzy5083 4 місяці тому

    i wish this worked for me i keep getting undefined

  • @aminamini9191
    @aminamini9191 2 роки тому

    ♥️♥️♥️

  • @shayanvaliyari8232
    @shayanvaliyari8232 Рік тому +1

    It could be better if u separated the introduction to .env and the usage guide into two even shorter videos just so people get faster answers from your channel

  • @BronteJack-o3l
    @BronteJack-o3l 17 днів тому

    Don Ports

  • @SuperYtc1
    @SuperYtc1 2 роки тому +1

    1) I don't care what your name is.
    2) I can read the title of the video, that's why I clicked on it.

  • @ruuuff
    @ruuuff Рік тому

    thank you!