dotenv path will not pick up .env files but sees everything else in the folder.
i have tried
dotenv.config({path:'relative path'})
dotenv.config({path:'absolute path'})
when i try the relative path it sees everything except .env file. like it dosen’t exist. i open it up in vs code / file explorer, it is very much there.
the structure is like this:
root:
modules
playwright-report
pupputeer
test-results
tests
.env
example.spec.ts
login.test.ts
tests-examples
.gitignore
package-lock.json
package.json
playwright.config.ts
login.test.ts code
import dotenv from 'dotenv';
dotenv.config({ path: './tests/.env' });
import { firefox, test } from "@playwright/test";
test("Login test demo", async () => {
const browser = await firefox.launch();
const context = await browser.newContext();
const page = await context.newPage();
console.log(process.env);
await page.goto("https://ecommerce-playground.lambdatest.io/");
await page.hover("//a[@data-toggle='dropdown']//span[contains(.,'My account')]");
await page.click("text=Login");
// await page.fill("input[name='email']", );
});
playwright.config.ts code
import type { PlaywrightTestConfig } from "@playwright/test";
const config: PlaywrightTestConfig = {
use: {
headless: false
},
testMatch: ["tests/login.test.ts"]
}
export default config;
package.json code:
{
"type": "commonjs",
"name": "scrape",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"start": "ts-node tests-examples/demo-todo-app.spec.ts"
},
"keywords": [],
"author": "",
"license": "ISC",
"description": "",
"devDependencies": {
"@playwright/test": "^1.46.1",
"@types/node": "^22.5.0"
},
"dependencies": {
"dotenv": "^16.4.5",
"ts-node": "^10.9.2"
}
}
i run npx playwright test
, to launch
6
To ensure the .env file is correctly formatted, double-check that the two lines defining GITHUB_TOKEN and GITHUB_USERNAME don’t have semicolons at the end. The correct format should look like this:
GITHUB_TOKEN=your_token_here
GITHUB_USERNAME=your_username_here
This small adjustment can make a big difference in how the environment variables are read and used by the application. It’s a common oversight that can lead to unexpected behavior, so it’s always worth verifying.