N
Nuxt2y ago
Talaxasy

How to use env variables inside /utils folder?

I want to access env variables from inside /utils folder for ex.: I have constants.ts file and there:
export const isDev = process.env.NODE_ENV === 'development';
export const isConsoleDebugSee = process.env.CONSOLE_DEBUG;
export const isConsoleDebug = process.env.CONSOLE_DEBUG
? process.env.CONSOLE_DEBUG === 'true'
: undefined;
export const isPreviewDebug = process.env.DISPLAY_PREVIEW_CONETENT
? process.env.DISPLAY_PREVIEW_CONETENT === 'true'
: undefined;
export const isConsole =
typeof isConsoleDebug !== 'undefined' ? isConsoleDebug : isDev;
export const isPreview =
typeof isPreviewDebug !== 'undefined' ? isPreviewDebug : isDev;
export const isDev = process.env.NODE_ENV === 'development';
export const isConsoleDebugSee = process.env.CONSOLE_DEBUG;
export const isConsoleDebug = process.env.CONSOLE_DEBUG
? process.env.CONSOLE_DEBUG === 'true'
: undefined;
export const isPreviewDebug = process.env.DISPLAY_PREVIEW_CONETENT
? process.env.DISPLAY_PREVIEW_CONETENT === 'true'
: undefined;
export const isConsole =
typeof isConsoleDebug !== 'undefined' ? isConsoleDebug : isDev;
export const isPreview =
typeof isPreviewDebug !== 'undefined' ? isPreviewDebug : isDev;
In a frontend console I see that CONSOLE_DEBUG always is undefined, but I have access to process.env.NODE_ENV
2 Replies
Talaxasy
TalaxasyOP2y ago
To be precise, I have this code: .env file:
CONSOLE_DEBUG="false"
CONSOLE_DEBUG="false"
src/utils/constants.ts:
export const isDev = process.env.NODE_ENV === 'development';
export const isConsoleDebug = process.env.CONSOLE_DEBUG
? process.env.CONSOLE_DEBUG === 'true'
: undefined;
export const isConsole =
typeof isConsoleDebug !== 'undefined' ? isConsoleDebug : isDev;
export const isDev = process.env.NODE_ENV === 'development';
export const isConsoleDebug = process.env.CONSOLE_DEBUG
? process.env.CONSOLE_DEBUG === 'true'
: undefined;
export const isConsole =
typeof isConsoleDebug !== 'undefined' ? isConsoleDebug : isDev;
src/utils/helpers.ts:
export const logger = isConsole
? console
: { info: () => {}, warn: () => {}, error: () => {} };
export const logger = isConsole
? console
: { info: () => {}, warn: () => {}, error: () => {} };
Then on the frontend I use my logger like this: logger.info('Page:', toJS(page.value));
alexcali
alexcali2y ago
Did you find a solution to this?
Want results from more Discord servers?
Add your server