Не удается получить доступ к переменным из файла .env

Рейтинг: 1Ответов: 1Опубликовано: 17.02.2023

Пытаюсь получить значения переменных из .env, но все переменные is not defined.

tsconfig.json

{
  "compilerOptions": {
    "target": "ESNext",
    "useDefineForClassFields": true,
    "lib": ["DOM", "DOM.Iterable", "ESNext"],
    "allowJs": false,
    "skipLibCheck": true,
    "esModuleInterop": false,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "module": "ESNext",
    "moduleResolution": "Node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx",
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["src", "./jest-setup.ts", "SsrRender.tsx"],
  "references": [{ "path": "./tsconfig.node.json" }]
}

vite.config.ts

import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import dotenv from 'dotenv';
import path from 'path';
import { CompileTsServiceWorker } from './src/utils/serviceWorker/compileTsServiceWorker';

dotenv.config({ path: path.join(__dirname, '..', '..', '.env') });

// https://vitejs.dev/config/
export default defineConfig({
  server: {
    port: Number(process.env.CLIENT_PORT) || 3000,
  },
  define: {
    __SERVER_PORT__: process.env.SERVER_PORT,
    __TELEGRAM_TOKEN__: process.env.TELEGRAM_TOKEN,
    __TELEGRAM_ID__: process.env.TELEGRAM_ID,
  },
  plugins: [react(), CompileTsServiceWorker()],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
    },
  },
});

client.d.ts

declare global {
  const __SERVER_PORT__: number;
  const __TELEGRAM_TOKEN__: string;
  const __TELEGRAM_ID__: string;
}
export {};

файл api.ts, в которым используются эти переменные

export const redirectURI = `http://localhost:${__SERVER_PORT__}/`;
export const telegramURL = `https://api.telegram.org/bot${__TELEGRAM_TOKEN__}/sendMessage?chat_id=${__TELEGRAM_ID__}&parse_mode=html`;

В итоге, все три переменные SERVER_PORT, TELEGRAM_TOKEN, TELEGRAM_ID is not defined.

Ответы

▲ 0

Проблема оказалась в ESLint, не видел глобальные переменные