Home > Software design >  HTML .gitignore
HTML .gitignore

Time:02-10

I currently have my api keys and list id sitting in my app.js file. I am trying to add those API keys to another file and hide the file using .gitignore. But how do I do that and even if I hide the file in the new secret file how do I get those keys back in my original app.js file

CodePudding user response:

What you should do is use an Environment File. Environment files start with .env and allow you to save things that are specific to your working environment (such as sensitive keys). If you are using nodejs, you can easily import the data from a .env file using require('dotenv').config(). These files are also ignored by the default .gitignore configuration.

CodePudding user response:

Firstly install a library called dotenv

npm i dotenv --save

Create a file called .env and place your API KEY in it and create .gitignore file place your .env in it

Example

Inside .env :

API_KEY= YOUR_APIKEY without quotation

Inside .gitignore:

.env

To use your API KEY inside app.js

Inside app.js:

require('dotenv').config()

let API_KEY = process.env.API_KEY;

CodePudding user response:

  1. Using .env file
  • Install the dotenv npm package

    npm install dotenv --save

  • Create a .env file at the root of your project and store your SECRET_KEY like shown below.

    SECRET_KEY="YOURSECRETKEYGOESHERE"

    To get the value from the key use the following code

    require('dotenv').config()

    console.log(process.env)

    For more information do check the enter image description here

    To get the prebuild template for .gitignore files for nodeJs or any kind of project do check the repository down below.

    https://github.com/github/gitignore

  • Related