JS Import Safety & Runtimes

Import praisonai safely in any JavaScript runtime and migrate from auto-loaded .env

import 'praisonai' runs no process.env reads and no dotenv.config() at load time, so it works in browsers, Electron renderers, webviews (Tauri), and React Native.

Diagram

Import Safety

📦 import ‘praisonai’

🛡️ No side effects

⏳ Credentials read lazily

✅ Client built on demand

Import safety

Importing praisonai no longer touches the runtime environment.

  • import 'praisonai' performs no process.env reads and no dotenv.config() at load time — safe in browsers, Electron renderers, webviews (Tauri), and React Native.
  • The old dotenv.config() side effect is gone. If you relied on it to auto-load .env in Node, call it yourself (see Migrating from auto-loaded .env).
  • The dotenv and node-fetch npm packages are no longer transitive dependencies of praisonai. Both were unused in the SDK source; dropping them improves tree-shaking and shrinks install size.

Do I still need dotenv?

Only .env files need a one-line change; every other credential path is untouched.

Diagram

export in shell / systemd / CI

.env file at project root

Per-agent apiKey / baseURL

Browser / webview / RN

How do you supply credentials?

Nothing to change — env vars still work at runtime

Install dotenv and call dotenv.config yourself

Pass them directly — no env vars needed

Bundle-time constants — see below

Quick Start

  1. Zero-config Node with shell env vars

    No code change needed when OPENAI_API_KEY is set in your shell, .zshrc, or systemd unit.

    ts
    import { Agent } from 'praisonai';
    
    const agent = new Agent({ instructions: 'You are a helpful assistant' });
    await agent.start('Hello');
  2. Node with a .env file

    Load .env yourself — praisonai no longer does it for you.

    ts
    import 'dotenv/config';                 // Load .env yourself — praisonai no longer does.
    import { Agent } from 'praisonai';
    
    const agent = new Agent({ instructions: 'You are a helpful assistant' });
    await agent.start('Hello');
  3. Browser / webview

    No process, no dotenv, no throw. Pass credentials per-agent.

    ts
    import { Agent } from 'praisonai';
    
    // Nothing at module load reaches for `process` or `.env`.
    const agent = new Agent({
      instructions: 'You are a helpful assistant',
      apiKey: import.meta.env.VITE_OPENAI_KEY,
    });
    await agent.chat('Hello from the browser!');

Migrating from auto-loaded .env

praisonai used to call dotenv.config() for you on import — now you call it once yourself.

Process env vars set in the shell, systemd, Docker, or CI still work with zero code changes. Only .env files loaded via the dotenv package need this one-line addition.

Before (v1.7.4 and earlier)
// .env was loaded for you the moment you imported the package.
import { Agent } from 'praisonai';
const agent = new Agent({ instructions: 'Hi' });
After (from PR #4416)
import 'dotenv/config';       // Load .env explicitly — praisonai no longer does this for you.
import { Agent } from 'praisonai';
const agent = new Agent({ instructions: 'Hi' });

Best Practices

Prefer shell/CI env vars in production

Set OPENAI_API_KEY in your shell, systemd unit, Docker, or CI. These work at runtime with no code changes and keep secrets out of your bundle.

Use dotenv only for local .env files

If you keep credentials in a project-root .env, install dotenv and call dotenv.config() (or import 'dotenv/config') at the very top of your entrypoint.

Pass credentials per-agent in browsers

Browsers and webviews have no process or .env. Provide apiKey (and baseURL/fetch when needed) directly on each Agent using bundle-time constants like import.meta.env.VITE_OPENAI_KEY.

Rely on lazy credential reads

praisonai reads OPENAI_API_KEY only when an OpenAI client is built, and LOGLEVEL only on first log. Importing the package never throws in runtimes without process.

The env-only client also rebuilds when OPENAI_API_KEY or OPENAI_BASE_URL changes between calls. A rotated key is picked up without a restart. See JS Credential Rotation for the full contract and resetOpenAIClient().


Related