Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@dotenvx/dotenvx

Package Overview
Dependencies
Maintainers
2
Versions
187
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@dotenvx/dotenvx

a better dotenv–from the creator of `dotenv`

  • 0.6.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
92K
increased by9.4%
Maintainers
2
Weekly downloads
 
Created
Source

dotenvx

a better dotenv–from the creator of dotenv.

 

Quickstart

brew install dotenvx/brew/dotenvx

 

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

More examples

  • Python 🐍
    $ echo "HELLO=World" > .env && echo 'import os;print("Hello " + os.getenv("HELLO", ""))' > index.py
    
    $ dotenvx run -- python3 index.py
    Hello World
    
  • PHP 🐘
    $ echo "HELLO=World" > .env && echo '<?php echo "Hello {$_SERVER["HELLO"]}\n";' > index.php
    
    $ dotenvx run -- php index.php
    Hello World
    
  • Ruby 💎
    $ echo "HELLO=World" > .env && echo 'puts "Hello #{ENV["HELLO"]}"' > index.rb
    
    $ dotenvx run -- ruby index.rb
    Hello World
    
  • Rust 🦀
    $ 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
    
  • Java ☕️
    $ 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
    
  • .NET 🔵
    $ dotnet new console -n HelloWorld -o HelloWorld
    $ cd HelloWorld
    $ echo "HELLO=World" > .env && echo 'Console.WriteLine($"Hello {Environment.GetEnvironmentVariable("HELLO")}");' > Program.cs && echo "HELLO=World" > .env
    
    $ dotenvx run -- dotnet run
    Hello World
    
  • Frameworks ▲
    $ dotenvx run -- next dev
    $ dotenvx run -- npm start
    $ dotenvx run -- bin/rails s
    $ dotenvx run -- php artisan serve
    
  • Docker 🐳
    $ 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"]
    
  • CI/CDs 🐙
    examples coming soon
    
  • Platforms
    examples coming soon
    
  • npx
    # alternatively use npx
    $ npx @dotenvx/dotenvx run -- node index.js
    $ npx @dotenvx/dotenvx run -- next dev
    $ npx @dotenvx/dotenvx run -- npm start
    
  • npm
    $ 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] injecting 1 environment variable from .env
    Hello World
    
  • Git
    # use as a git submodule
    $ git dotenvx run -- node index.js
    $ git dotenvx run -- next dev
    $ git dotenvx run -- npm start
    

 

Multiple Environments

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

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

$ dotenvx run --env-file=.env.production -- node index.js
Hello production
> ^^

More examples

  • multiple `.env` files
    $ echo "HELLO=local" > .env.local
    
    $ echo "HELLO=World" > .env
    
    $ dotenvx run --env-file=.env.local --env-file=.env -- node index.js
    Hello local
    
  • `--overload` flag
    $ echo "HELLO=local" > .env.local
    
    $ echo "HELLO=World" > .env
    
    $ dotenvx run --env-file=.env.local --env-file=.env --overload -- node index.js
    Hello World
    
  • `--verbose` flag
    $ echo "HELLO=production" > .env.production
    
    $ dotenvx run --env-file=.env.production --verbose -- node index.js
    [dotenvx][VERBOSE] injecting env from /path/to/.env.production
    [dotenvx][VERBOSE] HELLO set
    [dotenvx][INFO] injecting 1 environment variable from .env.production
    Hello production
    
  • `--debug` flag
    $ echo "HELLO=production" > .env.production
    
    $ dotenvx run --env-file=.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] injecting 1 environment variable from .env.production
    Hello production
    

 

Encryption

Encrypt your secrets to a .env.vault file.

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

$ echo "HELLO=production" > .env.production

$ dotenvx encrypt
[dotenvx][INFO] encrypted .env,.env.production to .env.vault
[dotenvx][INFO]
[dotenvx][INFO] try it out:
[dotenvx][INFO]
[dotenvx][INFO]     DOTENV_KEY='<DOTENV_KEY_ENVIRONMENT>' dotenvx run -- node index.js
[dotenvx][INFO]
[dotenvx][INFO] next:
[dotenvx][INFO]
[dotenvx][INFO]     1. commit .env.vault safely to code
[dotenvx][INFO]     2. set DOTENV_KEY on server (or ci)
[dotenvx][INFO]     3. push your code
[dotenvx][INFO]
[dotenvx][INFO] tips:
[dotenvx][INFO]
[dotenvx][INFO]     * .env.keys file holds your decryption DOTENV_KEYs
[dotenvx][INFO]     * DO NOT commit .env.keys to code
[dotenvx][INFO]     * share .env.keys file over secure channels only
> :-]

 

Then load env from encrypted .env.vault file

$ DOTENV_KEY='dotenv://:key_abc123@dotenvx.com/vault/.env.vault?environment=production' dotenvx run -- node index.js
[dotenvx][INFO] injecting 1 environment variable from encrypted .env.vault
Hello production

> :-]

More examples

  • AWS Lambda
    coming soon
    
  • Digital Ocean
    coming soon
    
  • Docker
    coming soon
    
  • Fly.io
    coming soon
    
  • Heroku
    coming soon
    
  • Laravel Forge
    coming soon
    
  • Netlify
    coming soon
    
  • Railway
    coming soon
    
  • Render
    coming soon
    
  • Vercel
    coming soon
    
  • CircleCI
    coming soon
    
  • GitHub Actions
    coming soon
    

 


Usage

Guide

Begin by creating a simple 'hello world' program.

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

Run it.

$ node index.js
Hello undefined

Run it with dotenvx.

$ dotenvx run -- node index.js
[dotenvx][WARN] ENOENT: no such file or directory, open '/../../.env'
Hello undefined

It warns you when there is no .env file (pass the --quiet flag to suppress these warnings).

Create the .env file.

# env
JELLO="World"

Run it again.

$ dotenvx run -- node index.js
[dotenvx][INFO] injecting 0 environment variables from .env
Hello undefined

Hrm, still undefined. Pass the --debug flag to debug the issue. I'll give you a hint: 🍮

$ dotenvx run --debug -- node index.js
[dotenvx][VERBOSE] Loading env from /../../.env
[dotenvx][DEBUG] Reading env from /../../.env
[dotenvx][DEBUG] Parsing env from /../../.env
[dotenvx][DEBUG] {"JELLO":"World"}

# Oops, HELLO not JELLO ^^

Fix your .env file.

# .env
HELLO="World"

One last time. Le tired.

$ dotenvx run -- node index.js
[dotenvx][INFO] injecting 1 environment variable from .env
Hello World

🎉 It worked!

 

Contributing

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

Keywords

FAQs

Package last updated on 29 Nov 2023

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc