Skip to main content

.env

Create a .env file

Note: The .env file should not be committed to your repository. Ensure it is added to your .gitignore file.

Install dotenv

yarn add dotenv -D -W

If you've added the dotenv package and the token value is not being retrieved, ensure the following steps are correctly implemented:

  1. Install dotenv: Confirm that you've installed dotenv in your project:

    npm install dotenv
  2. Create a .env file: Ensure you have a .env file in your project root directory with the following content (adjust the token value accordingly):

    GITHUB_TOKEN=your_actual_token_here
  3. Load dotenv early in your code: Make sure you load the environment variables at the very beginning of your application, typically in your main entry file (e.g., index.js or app.js). Add the following line:

    require('dotenv').config();
  4. Access the token: Since typescript compiles into javascript, you should still ensure your typescript files are resolving environment variables correctly. Your typecasting, if necessary, should accurately reflect the types:

    const GITHUB_TOKEN: string | undefined = process.env.GITHUB_TOKEN;

    if (!GITHUB_TOKEN) {
    throw new Error('GITHUB_TOKEN is not defined in your environment variables.');
    }
  5. Check for startup issues: Make sure your application is starting correctly with no errors and that it has access to the .env file.

  6. Environment-specific settings: Confirm that the .env file is not ignored (check .gitignore) and is accessible in your development environment. Note that for production environments, environment variables are typically set at the system level rather than in a .env file.

If, after following these steps, the token is still not accessible, verify the paths and files are correctly set up and that no part of your code or deployment process inhibits reading the .env file.