deadcoder0904
deadcoder0904

Reputation: 8683

How can I use SECRET_ENV in npm scripts inside of package.json?

I have a secret key called API_KEY that I want to access inside of package.json's scripts.

package.json

{
   "scripts": {
      "start": "web-ext run --api-key=API_KEY"
   }
}

My .env file contains API_KEY:

API_KEY=abc123

How can I access the value of API_KEY inside package.json's scripts while still keeping it a secret because I need to push package.json publicly?

Currently, I do the following which works but not cross-platform:

package.json

{
   "scripts": {
      "start": "web-ext run --api-key=$API_KEY"
   }
}

And when running start script I do it like:

API_KEY=abc123 npm start

This works thanks to Bash Programming Language but it doesn't work on Windows. I need to replace $API_KEY in start script with %API_KEY%. But I want it to be cross-platform. Is there any other way?

Upvotes: 6

Views: 3649

Answers (4)

Lukas-Sachse
Lukas-Sachse

Reputation: 1

Cross-Plattform: Using dotenv-cli & cross-var

Disclaimer: Only tested with Windows 11 (PowerShell 7 and CMD) so far.

Requires dotenv-cli and cross-var (or a fork like x-var)

dotenv-cli: Makes the values from .env accessible inside the scripts-section.

cross-var: Makes it usable cross-plattform with $API_KEY when between \" \" like \"$API_KEY\".

.env

API_KEY='sdf8879123sdfi'
API_ENDPOINT='https://api.example.com'

packages.json

{
  "scripts": {
    "check-env": "dotenv -- cross-var echo \"$API_ENDPOINT\"",
    "start-v1": "dotenv -- cross-var YOUR-CUSTOM-START-COMMAND --api=\"$API_ENDPOINT\" --key=\"$API_KEY\""
    "start-v2": "dotenv -- cross-var \" YOUR-CUSTOM-START-COMMAND --api=$API_ENDPOINT --api-key=$API_KEY \""

 }
  "devDependencies": {
    "cross-var": "^1.1.0",
    "dotenv-cli": "^7.0.0",
  }
}

Syntax-Explanation

dotenv --: The -- belongs to dotenv-cli, and it's used as a separator. It's helpful to specify which flags belong to dotenv like dotenv -e .env.local -- [...] and which belong to the rest.

Upvotes: 0

blub
blub

Reputation: 9187

The only other viable answer to this I found so far is a bit hacky:

{
   "scripts": {
      "start": "web-ext run --api-key=$(grep API_KEY .env | cut -d '=' -f2)"
   }
}

[https://stackoverflow.com/a/58038814/1822977]

Upvotes: 2

elingerojo
elingerojo

Reputation: 374

For cross platform

1) You can use 'npm env-cmd' as a devDependencies.

Setting the environment from a file

Usage

Environment file ./.env

# This is a comment
API_KEY=abc123

Package.json

{
  "scripts": {
    "start": "env-cmd web-ext run"
  }
}

2) You can use 'npm cross-env' as a devDependencies.

Run scripts that set and use environment variables across platforms

Usage

{
  "scripts": {
    "start": "cross-env API_KEY=abc123 web-ext run"
  }
}

For Windows only

You can try something like this:

cmd /C "set API_KEY=abc123 && npm start"

As Viper_Sb says here:

/C exits the new cmd right away after running, if you produce output with the new one it will still be visible in the parent window.

You can opt to use /K in which case the new cmd window stays open at the end of the run.

Upvotes: 1

massam
massam

Reputation: 57

You can simply require "dotenv" lib, and access var from process.env.{SOME_KEY}

Upvotes: -1

Related Questions