0.35.1 β€’ Published 8 days ago

@dotenvx/dotenvx v0.35.1

Weekly downloads
-
License
MIT
Repository
github
Last release
8 days ago

dotenvx

a better dotenv–from the creator of dotenv.

  • run anywhere (cross-platform)
  • multi-environment
  • encrypted envs

Β 

Quickstart

Install and use it in code just like dotenv.

npm install @dotenvx/dotenvx --save
// index.js
require('@dotenvx/dotenvx').config()

console.log(`Hello ${process.env.HELLO}`)

Β 

Or install globally

brew install dotenvx/brew/dotenvx

Intall globally as a cli to unlock dotenv for ANY language, framework, or platform. πŸ’₯

I am using (and recommending) this approach going forward. – motdotla

Β 

Run Anywhere

$ echo "HELLO=World" > .env
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ node index.js
Hello undefined

$ dotenvx run -- node index.js
Hello World
> :-D

see extended quickstart guide

More examples

  • // package.json
    {
      "type": "module",
      "dependencies": {
        "chalk": "^5.3.0"
      }
    }
    // index.ts
    import chalk from 'chalk'
    console.log(chalk.blue(`Hello ${process.env.HELLO}`))
    $ npm install
    $ echo "HELLO=World" > .env
    
    $ dotenvx run -- npx tsx index.ts
    Hello World
  • $ echo "HELLO=World" > .env
    $ echo "console.log('Hello ' + Deno.env.get('HELLO'))" > index.ts
    
    $ deno run --allow-env index.ts
    Hello undefined
    
    $ dotenvx run -- deno run --allow-env index.ts
    Hello World
  • $ echo "HELLO=Test" > .env.test
    $ echo "console.log('Hello ' + process.env.HELLO)" > index.js
    
    $ bun index.js
    Hello undefined
    
    $ dotenvx run -f .env.test -- bun index.js
    Hello Test
  • $ echo "HELLO=World" > .env
    $ echo 'import os;print("Hello " + os.getenv("HELLO", ""))' > index.py
    
    $ dotenvx run -- python3 index.py
    Hello World

    see extended python guide

  • $ echo "HELLO=World" > .env
    $ echo '<?php echo "Hello {$_SERVER["HELLO"]}\n";' > index.php
    
    $ dotenvx run -- php index.php
    Hello World

    see extended php guide

  • $ echo "HELLO=World" > .env
    $ echo 'puts "Hello #{ENV["HELLO"]}"' > index.rb
    
    $ dotenvx run -- ruby index.rb
    Hello World

    see extended ruby guide

  • $ echo "HELLO=World" > .env
    $ echo 'package main; import ("fmt"; "os"); func main() { fmt.Printf("Hello %s\n", os.Getenv("HELLO")) }' > main.go
    
    $ dotenvx run -- go run main.go
    Hello World

    see extended go guide

  • $ echo "HELLO=World" > .env
    $ echo 'fn main() {let hello = std::env::var("HELLO").unwrap_or("".to_string());println!("Hello {hello}");}' > src/main.rs
    
    $ dotenvx run -- cargo run
    Hello World

    see extended rust guide

  • $ echo "HELLO=World" > .env
    $ echo 'public class Index { public static void main(String[] args) { System.out.println("Hello " + System.getenv("HELLO")); } }' > index.java
    
    $ dotenvx run -- java index.java
    Hello World
  • $ dotnet new console -n HelloWorld -o HelloWorld
    $ cd HelloWorld
    $ echo "HELLO=World" > .env
    $ echo 'Console.WriteLine($"Hello {Environment.GetEnvironmentVariable("HELLO")}");' > Program.cs
    
    $ dotenvx run -- dotnet run
    Hello World
  • $ echo "HELLO=World" > .env
    
    $ dotenvx run --quiet -- sh -c 'echo Hello $HELLO'
    Hello World
  • # run every day at 8am
    0 8 * * * dotenvx run -- /path/to/myscript.sh
  • $ dotenvx run -- next dev
    $ dotenvx run -- npm start
    $ dotenvx run -- bin/rails s
    $ dotenvx run -- php artisan serve

    see framework guides

  • $ docker run -it --rm -v $(pwd):/app dotenv/dotenvx run -- node index.js

    Or in any image:

    FROM node:latest
    RUN echo "HELLO=World" > .env && echo "console.log('Hello ' + process.env.HELLO)" > index.js
    RUN curl -fsS https://dotenvx.sh/ | sh
    CMD ["dotenvx", "run", "--", "echo", "Hello $HELLO"]

    see docker guide

  • name: build
    on: [push]
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v3
        - uses: actions/setup-node@v3
          with:
            node-version: 16
        - run: curl -fsS https://dotenvx.sh/ | sh
        - run: dotenvx run -- node build.js
          env:
            DOTENV_KEY: ${{ secrets.DOTENV_KEY }}

    see github actions guide

  • # heroku
    heroku buildpacks:add https://github.com/dotenvx/heroku-buildpack-dotenvx
    
    # docker
    RUN curl -fsS https://dotenvx.sh/ | sh
    
    # vercel
    npm install @dotenvx/dotenvx --save

    see platform guides

  • // pm2
    "scripts": {
      "start": "dotenvx run -- pm2-runtime start ecosystem.config.js --env production"
    },

    see process manager guides

  • # alternatively use npx
    $ npx @dotenvx/dotenvx run -- node index.js
    $ npx @dotenvx/dotenvx run -- next dev
    $ npx @dotenvx/dotenvx run -- npm start
  • $ npm install @dotenvx/dotenvx --save
    {
      "scripts": {
        "start": "./node_modules/.bin/dotenvx run -- node index.js"
      },
      "dependencies": {
        "@dotenvx/dotenvx": "^0.5.0"
      }
    }
    $ npm run start
    
    > start
    > ./node_modules/.bin/dotenvx run -- node index.js
    
    [dotenvx][info] loading env (1) from .env
    Hello World
  • # use as a git submodule
    $ git dotenvx run -- node index.js
    $ git dotenvx run -- next dev
    $ git dotenvx run -- npm start
  • Reference and expand variables already on your machine for use in your .env file.

    # .env
    USERNAME="username"
    DATABASE_URL="postgres://${USERNAME}@localhost/my_database"
    // index.js
    console.log('DATABASE_URL', process.env.DATABASE_URL)
    $ dotenvx run --debug -- node index.js
    [dotenvx@0.14.1] injecting env (2) from .env
    DATABASE_URL postgres://username@localhost/my_database
  • Add the output of a command to one of your variables in your .env file.

    # .env
    DATABASE_URL="postgres://$(whoami)@localhost/my_database"
    // index.js
    console.log('DATABASE_URL', process.env.DATABASE_URL)
    $ dotenvx run --debug -- node index.js
    [dotenvx@0.14.1] injecting env (1) from .env
    DATABASE_URL postgres://yourusername@localhost/my_database

Β 

Multiple Environments

Create a .env.production file and use -f to load it. It's straightforward, yet flexible.

$ echo "HELLO=production" > .env.production
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx run -f .env.production -- node index.js dotenvx loading env (1) from .env.production Hello production

^^

More examples

  • $ echo "HELLO=local" > .env.local
    
    $ echo "HELLO=World" > .env
    
    $ dotenvx run -f .env.local -f .env -- node index.js
    [dotenvx][info] loading env (1) from .env.local,.env
    Hello local
  • $ echo "HELLO=local" > .env.local
    
    $ echo "HELLO=World" > .env
    
    $ dotenvx run -f .env.local -f .env --overload -- node index.js
    [dotenvx][info] loading env (1) from .env.local,.env
    Hello World
  • $ echo "HELLO=production" > .env.production
    
    $ dotenvx run -f .env.production --verbose -- node index.js
    [dotenvx][verbose] injecting env from /path/to/.env.production
    [dotenvx][verbose] HELLO set
    [dotenvx][info] loading env (1) from .env.production
    Hello production
  • $ echo "HELLO=production" > .env.production
    
    $ dotenvx run -f .env.production --debug -- node index.js
    [dotenvx][debug] configuring options
    [dotenvx][debug] {"envFile":[".env.production"]}
    [dotenvx][verbose] injecting env from /path/to/.env.production
    [dotenvx][debug] reading env from /path/to/.env.production
    [dotenvx][debug] parsing env from /path/to/.env.production
    [dotenvx][debug] {"HELLO":"production"}
    [dotenvx][debug] writing env from /path/to/.env.production
    [dotenvx][verbose] HELLO set
    [dotenvx][debug] HELLO set to production
    [dotenvx][info] loading env (1) from .env.production
    Hello production
  • Use --quiet to suppress all output (except errors).

    $ echo "HELLO=production" > .env.production
    
    $ dotenvx run -f .env.production --quiet -- node index.js
    Hello production
  • Set --log-level to whatever you wish. For example, to supress warnings (risky), set log level to error:

    $ echo "HELLO=production" > .env.production
    
    $ dotenvx run -f .env.production --log-level=error -- node index.js
    Hello production

    Available log levels are error, warn, info, verbose, debug, silly

Β 

Encryption

Encrypt your secrets to a .env.vault file and load from it (recommended for production and ci).

$ echo "HELLO=World" > .env
$ echo "HELLO=production" > .env.production
$ echo "console.log('Hello ' + process.env.HELLO)" > index.js

$ dotenvx encrypt dotenvx encrypted to .env.vault (.env,.env.production) dotenvx keys added to .env.keys (DOTENV_KEY_PRODUCTION,DOTENV_KEY_PRODUCTION)

$ DOTENV_KEY='<dotenv_key_production>' dotenvx run -- node index.js dotenvx loading env (1) from encrypted .env.vault Hello production ^ :-]

More examples

* <details><summary>AWS Lambda</summary><br>

  ```bash
  coming soon
  • coming soon
  • Add the dotenvx binary to your Dockerfile

    # Install dotenvx
    RUN curl -fsS https://dotenvx.sh/ | sh

    Use it in your Dockerfile CMD

    # Prepend dotenvx run
    CMD ["dotenvx", "run", "--", "node", "index.js"]

    see docker guide

  • Add the dotenvx binary to your Dockerfile

    # Install dotenvx
    RUN curl -fsS https://dotenvx.sh/ | sh

    Use it in your Dockerfile CMD

    # Prepend dotenvx run
    CMD ["dotenvx", "run", "--", "node", "index.js"]

    see fly guide

  • Add the buildpack, installing the dotenvx binary to your heroku deployment.

    heroku buildpacks:add https://github.com/dotenvx/heroku-buildpack-dotenvx

    Use it in your Procfile.

    web: dotenvx run -- node index.js

    see heroku guide

  • coming soon
  • Add the dotenvx npm module

    npm install @dotenvx/dotenvx --save

    Use it in your package.json scripts

    "scripts": {
      "dotenvx": "dotenvx",
      "dev": "dotenvx run -- next dev --turbo",
      "build": "dotenvx run -- next build",
      "start": "dotenvx run -- next start"
    },

    see netlify guide

  • Add the dotenvx binary to your Dockerfile

    # Install dotenvx
    RUN curl -fsS https://dotenvx.sh/ | sh

    Use it in your Dockerfile CMD

    # Prepend dotenvx run
    CMD ["dotenvx", "run", "--", "node", "index.js"]

    see railway guide

  • coming soon
  • Add the dotenvx npm module

    npm install @dotenvx/dotenvx --save

    Use it in your package.json scripts

    "scripts": {
      "dotenvx": "dotenvx",
      "dev": "dotenvx run -- next dev --turbo",
      "build": "dotenvx run -- next build",
      "start": "dotenvx run -- next start"
    },

    see vercel guide

  • coming soon
  • Add the dotenvx binary to GitHub Actions

    name: build
    on: [push]
    jobs:
      build:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v3
        - uses: actions/setup-node@v3
          with:
            node-version: 16
        - run: curl -fsS https://dotenvx.sh/ | sh
        - run: dotenvx run -- node build.js
          env:
            DOTENV_KEY: ${{ secrets.DOTENV_KEY }}

    see github actions guide

Β 

Hub

Integrate tightly with GitHub πŸ™ and as a team

$ dotenvx hub login
$ dotenvx hub push

beta: more details coming soon.

Β 

More features

Keep your .env files safe

Convenience

Β 

Guides

Β 

FAQ

Why am I getting the error node: .env: not found?

You are using Node 20 or greater and it adds a differing implementation of --env-file flag support. Rather than warn on a missing .env file (like dotenv has historically done), it raises an error: node: .env: not found.

This fix is easy. Replace --env-file with -f.

# from this:
./node_modules/.bin/dotenvx run --env-file .env -- yourcommand
# to this:
./node_modules/.bin/dotenvx run -f .env -- yourcommand

more context

Β 

Contributing

You can fork this repo and create pull requests or if you have questions or feedback:

0.35.1

8 days ago

0.35.0

8 days ago

0.34.0

10 days ago

0.33.1

12 days ago

0.33.0

12 days ago

0.32.0

18 days ago

0.30.2

19 days ago

0.31.1

19 days ago

0.31.0

19 days ago

0.30.1

19 days ago

0.30.0

21 days ago

0.29.2

22 days ago

0.29.0

23 days ago

0.29.1

23 days ago

0.28.0

24 days ago

0.27.2

25 days ago

0.27.1

1 month ago

0.27.0

1 month ago

0.26.0

1 month ago

0.25.1

1 month ago

0.25.0

2 months ago

0.24.0

2 months ago

0.23.0

2 months ago

0.21.0

2 months ago

0.22.0

2 months ago

0.20.1

2 months ago

0.20.0

2 months ago

0.19.0

2 months ago

0.19.1

2 months ago

0.18.0

2 months ago

0.20.2

2 months ago

0.17.1

2 months ago

0.17.0

2 months ago

0.16.0

2 months ago

0.16.1

2 months ago

0.15.4

3 months ago

0.15.2

3 months ago

0.15.3

3 months ago

0.15.1

3 months ago

0.15.0

3 months ago

0.13.0

3 months ago

0.14.0

3 months ago

0.14.1

3 months ago

0.12.0

3 months ago

0.11.0

3 months ago

0.10.6

3 months ago

0.10.4

3 months ago

0.10.5

3 months ago

0.10.3

3 months ago

0.10.1

3 months ago

0.10.2

3 months ago

0.10.0

3 months ago

0.9.0

3 months ago

0.8.4

4 months ago

0.8.3

4 months ago

0.8.1

4 months ago

0.8.0

4 months ago

0.8.2

4 months ago

0.7.2

4 months ago

0.7.1

4 months ago

0.7.4

4 months ago

0.7.3

4 months ago

0.6.7

5 months ago

0.6.6

5 months ago

0.6.9

5 months ago

0.6.8

5 months ago

0.6.10

5 months ago

0.6.12

5 months ago

0.6.11

5 months ago

0.6.13

5 months ago

0.6.5

5 months ago

0.6.4

5 months ago

0.6.3

5 months ago

0.6.2

5 months ago

0.6.1

5 months ago

0.6.0

5 months ago

0.5.0

5 months ago

0.4.0

5 months ago

0.3.9

5 months ago

0.3.8

5 months ago

0.3.7

5 months ago

0.3.6

5 months ago

0.3.5

5 months ago

0.3.4

5 months ago

0.3.2

5 months ago

0.3.0

5 months ago

0.2.24

5 months ago

0.2.23

5 months ago