So basically I'm using ES modules in node and everything is working fine until I tried to moved my .env file from the root of the folder to a config folder and now node can't locate it, even if I specify the path within the dotenv.config() options, like so:
import { config } from "dotenv";
config({path: './config/.env'})
my console throws this:
Error: Expected token to be set for this request, but none was presentat RequestManager.resolveRequest (file:///C:/Users/macfi/OneDrive/Documents/GitHub/new_discord_bot/node_modules/@discordjs/rest/dist/index.mjs:734:15)at RequestManager.queueRequest (file:///C:/Users/macfi/OneDrive/Documents/GitHub/new_discord_bot/node_modules/@discordjs/rest/dist/index.mjs:706:46)at REST.raw (file:///C:/Users/macfi/OneDrive/Documents/GitHub/new_discord_bot/node_modules/@discordjs/rest/dist/index.mjs:861:32)at REST.request (file:///C:/Users/macfi/OneDrive/Documents/GitHub/new_discord_bot/node_modules/@discordjs/rest/dist/index.mjs:857:33)at REST.put (file:///C:/Users/macfi/OneDrive/Documents/GitHub/new_discord_bot/node_modules/@discordjs/rest/dist/index.mjs:851:17)at main (file:///C:/Users/macfi/OneDrive/Documents/GitHub/new_discord_bot/src/index.js:101:16)at file:///C:/Users/macfi/OneDrive/Documents/GitHub/new_discord_bot/src/index.js:110:1at ModuleJob.run (node:internal/modules/esm/module_job:198:25)at async Promise.all (index 0)at async ESMLoader.import (node:internal/modules/esm/loader:385:24)
Just so it's clear, if I keep the .env file in the root, everything works, like this:
dotenv.config()
Can't really find anything that helpful online about it, the dotenv docs only display a custom path example using CommonJS so yeah. Appreciate it if anyone has any insight to share.
I tried using the node path module and the __dirname but apparently those get disabled if you enable ES Modules
The below worked for me using ES Modules:
import dotenv from 'dotenv'
dotenv.config({ path: '../.env' })
const envVar = process.env.ENV_VAR
I slightly adapted the code from mjrussell91 to use an .env file in a .auth subfolder to store credentials for Playwright.
import dotenv from 'dotenv';
dotenv.config({ path: '.auth/.env' });
// Use case: Playwright script
// ...
await page.getByPlaceholder('Password').fill(process.env.SECRET_PASSWORD!);
// ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With