.env
Create a .env file
Note: The
.envfile should not be committed to your repository. Ensure it is added to your.gitignorefile.
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:
-
Install dotenv: Confirm that you've installed
dotenvin your project:npm install dotenv -
Create a
.envfile: Ensure you have a.envfile in your project root directory with the following content (adjust the token value accordingly):GITHUB_TOKEN=your_actual_token_here -
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.jsorapp.js). Add the following line:require('dotenv').config(); -
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.');
} -
Check for startup issues: Make sure your application is starting correctly with no errors and that it has access to the
.envfile. -
Environment-specific settings: Confirm that the
.envfile 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.envfile.
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.