dotenv-flow icon indicating copy to clipboard operation
dotenv-flow copied to clipboard

how to load with import?

Open ralyodio opened this issue 2 years ago • 5 comments

Is this correct?

import cfg from 'dotenv-flow';

cfg.config();

ralyodio avatar Apr 11 '22 09:04 ralyodio

Using the same way as documented for dotenv (see here) seems to work just fine:

// As early as possible
import 'dotenv-flow/config';

lucsky avatar May 11 '22 09:05 lucsky

@lucsky that wouldn't work unless https://github.com/kerimdzhanov/dotenv-flow/pull/57 is merged

perrin4869 avatar Sep 05 '22 07:09 perrin4869

@perrin4869 weird, I guess I'm lucky then because it works perfectly fine for me in two different projects... 🤔

lucsky avatar Sep 05 '22 07:09 lucsky

Do you have type module in package.json?

perrin4869 avatar Sep 05 '22 08:09 perrin4869

This worked for me in a ESM file:

import { foo } from "foo";
import { bar } from "./path/to/bar";

(await import("dotenv-flow")).config();

console.log(process.env.HELLO);

Because static imports are async by nature, I’m not sure if placing await before imports will guarantee correct process.env values in foo and ./path/to/bar. Needs checking.

kachkaev avatar Sep 14 '22 12:09 kachkaev

FWIW: even though #57 hasn't been merged, I've had luck using this within an ES module:

import "dotenv-flow/config.js";

The ".js" is necessary because of this (see 2nd and 3rd bullet points). One of those tricky/annoying differences with ES modules in a pure Node context versus the Webpack context many of us are more familiar with.

I'm also able to get @kachkaev's method to work, but other imported module files are only able to access the vars if they're imported after dotenv and also use dynamic import():

// probably doesn't matter where this goes since it's a package and doesn't use process.env internally
const foo = await import("foo");

(await import("dotenv-flow")).config(); // has to come before the import below
const bar = await import("./path/to/bar.js"); // process.env used in this file

MastaAaron avatar Feb 10 '23 16:02 MastaAaron

I think this is because nodejs support that, check https://nodejs.org/api/esm.html#commonjs-namespaces

When importing a CommonJS module, it can be reliably imported using the ES module default import or its corresponding sugar syntax:

qiulang avatar Jul 18 '23 03:07 qiulang

My config/env.ts file doesn't import anything else so this works for me to set the default NODE_ENV in an ESM package (with `"type": "module" in package.json):

if (!process.env.NODE_ENV) {
  process.env.NODE_ENV = "development";
}
(await import("dotenv-flow")).config();

However the following would not work in an ESM package:

(await import("dotenv-flow")).config({
  node_env: process.env.NODE_ENV,
  default_node_env: "development",
});

waynebloss avatar Aug 06 '23 20:08 waynebloss

Hello gents,

Sorry for being late to the party 😅,

Let me check #57 by @perrin4869 (thanks a lot for the PR!), for now, you can try this:

import * as dotenvFlow from 'dotenv-flow';
dotenvFlow.config();

I'll update the README appropriately once we check and merge the PR (* as may not be needed after merging).

kerimdzhanov avatar Aug 14 '23 17:08 kerimdzhanov