47

I have a simple requirement: In my npm scripts package.json file I have the line:

{
    "scripts": {
        "example": "some-lib --argument --domain \"https://tld.com\""
    }
}

Now I want the "domain" to be factored out.

First try is to use $npm_package_config, which works:

{
    "config": {
        "domain": "https://tld.com"
    },
    "scripts": {
        "example": "some-lib --argument --domain \"$npm_package_config_domain\""
    }
}

But I want the domain loaded from an local .env file.

I did not find any solution out there to read the contents of an env file inside npm scripts on the command line.

Can somebody give me a hint to a possible solution for this problem?

RobC
  • 22,977
  • 20
  • 73
  • 80
David Vielhuber
  • 3,253
  • 3
  • 29
  • 34

5 Answers5

23

Short answer: There's no terse way to achieve this that works cross-platform, as per your second example which references the $npm_package_config variable.

For a cross-platform solution, i.e. one that works successfully on both *nix and Windows platforms - whereby the default shell utilized by npm scripts is either sh or cmd respectively, you'll need to execute your command (i.e. the one which is currently defined in your npm-script) via a nodejs helper script. Essentially, your nodejs script will need to:

The nodejs helper script can then be invoked via your npm-script.

The following describes how to achieve a solution that runs cross-platform.


Solution

1. The .env file

Firstly, lets assume we have a .env file residing in the root of our project directory. The .env file contains the following entry:

DOMAIN=https://tld.com

2. Install

The following nodejs script utilizes the dotenv package to load the environment variable(s) from the .env file. We'll need to install it. To do this cd to your project directory and run the following command:

npm i -D dotenv

3. The Node.js script (some-lib-cmd.js)

Next create a nodejs script as follows. Let's name the file some-lib-cmd.js and save it in the root of the project directory:

// Requirements...
require('dotenv').config();
const execSync = require('child_process').execSync;
const path = require("path");

/**
 * Creates a path to an executable in the node_modules/.bin directory. Each
 * path segment is joined with the appropriate platform-specific separator as
 * a delimiter.
 * @param {String} cmd The name of the executable.
 * @returns {String} The path to the executable.
 */
function getBinFile(cmd) {
  return path.join('node_modules', '.bin', cmd);
}

// Execute the command...
execSync(`${getBinFile('some-lib')} --argument --domain ${process.env.DOMAIN}`, { stdio: [0, 1, 2] });

Notes:

  • If your .env file does not reside in the root of our project directory along with some-lib-cmd.js, then you can utilize dotenv's path option to define a custom path to the location of your .env file instead. For example:

    require('dotenv').config({ path: 'path/to/another/folder/' })
    
  • To reference the DOMAIN variable from within the nodejs script we utilize process.env, i.e. process.env.DOMAIN.

4. package.json

In the scripts section of your package.json define the following script:

"scripts": {
  "example": "node some-lib-cmd"
}

Note: If you have chosen to save some-lib-cmd.js elsewhere, i.e. not in the in the root of your project directory, then redefine the path in your example script as necessary. For instance:

"scripts": {
  "example": "node path/to/some/folder/some-lib-cmd"
}
RobC
  • 22,977
  • 20
  • 73
  • 80
  • 1
    Thanks RobC for your detailled help. I just published a package for this problem, have a look at https://github.com/vielhuber/from-env – David Vielhuber Jan 16 '19 at 21:20
  • 3
    @DavidVielhuber https://github.com/toddbluhm/env-cmd/ has been doing this for awhile? – Akkuma Jul 15 '19 at 18:24
  • @DavidVielhuber - You say _"can you give an example how ..."_ Is this a question for me or Akkuma ? b.t.w Briefly looking at 'env-cmd' it doesn't seem to provide a bulltin solution to use a variable inside your npm script as Akkuma suggests. – RobC Jul 17 '19 at 07:03
  • Sorry, wrong thread. @Akkuma can you give an example how env-cmd uses environment variables inside a npm script? I don't think thats possible. – David Vielhuber Jul 17 '19 at 07:46
  • `"scripts": { "webhooks:dev-rg": "env-cmd --use-shell 'ts-node src/MyUtils/webhooks_register.ts https://${DEV_RESOURCEGROUP}.azurewebsites.net'"...` – Piran Mar 02 '21 at 15:41
15

A much easier solution is to just use bash to parse the env file in-line and extract the variable you want:

Assuming your .env file looks like:

DOMAIN=https://tld.com
"scripts": {
  "example": "some-lib --argument --domain $(grep DOMAIN .env | cut -d '=' -f2)"
} 
Kabir Sarin
  • 18,092
  • 10
  • 50
  • 41
  • 8
    However, not so cool when cross-platform is a requirement as this fails in non-bash shells - notably windows cmd.exe – RobC Sep 21 '19 at 20:31
  • This is quite old yet still useful to me. Just wondering if there are any enhancements to this? Also is it possible to supply a default value for the env var, just in case it’s missing in the env file or the file doesn’t exist. I know I can do ${DOMAIN:-mydomain} but what is the syntax in this use case? – Muhammad Ahmod Sep 03 '20 at 19:27
  • [This answer](https://stackoverflow.com/a/20909045/1181570) has some enhancements, such as ignoring lines beginning with #, handling values with spaces in them, and loading an entire .env file instead of just one variable – Kabir Sarin Apr 24 '22 at 14:44
  • `cut: invalid field value ')'` error in Windows Git Bash. – thdoan Jul 07 '23 at 01:53
15

Let's say you have a .env file like this:

DOMAIN="https://tld.com"

Then, you can use source in your command, like so:

{
    // ...
    "scripts": {
        "example": "source .env && some-lib --argument --domain $DOMAIN"
    }
}

This won't work in Windows by default, but if you have Git Bash installed, then you can configure npm to use it to run commands with e.g. (from here):

npm config set script-shell "C:\\Program Files\\git\\bin\\bash.exe"
L.M
  • 303
  • 2
  • 8
  • 3
    I had to specify the path to the env file as `./.env` or else I got a **file not found** error. As a side note, if you need to persist these variables into the node process subshell you can add `set -a &&` before the `source` command to automatically export all the vars it contains. – Besworks Apr 26 '22 at 15:10
  • Please mind that if you run this setup as part of an npm script, you have to specify path to `.env` file relative to `package.json`, not to the file in which your script is written – Ilya Kushlianski Aug 01 '22 at 20:39
  • 2
    Using `source` in a npm script returns `sh: 1: source: not found`. I'm on Ubuntu. – Fred Sep 25 '22 at 00:50
  • 1
    I believe `source` can be replaced by simply `.`. @Fred, try this :-) – L.M Sep 25 '22 at 21:28
  • Thanks for the hint! I'll try. Using `env-cmd` npm package right now and it works fine :-) – Fred Sep 26 '22 at 21:42
  • Is there a reason why this is being up-voted when it isn't cross platform? Isn't that the point of using node? Should support Windows and Linux – TamusJRoyce Aug 22 '23 at 16:22
7

There is a library that does exactly what you want to follow 12factor app best practices: the dotenv package from npmjs.org

https://www.npmjs.com/package/dotenv

FTA:

$ node -r dotenv/config your_script.js dotenv_config_path=/custom/path/to/your/env/vars
Jack Murphy
  • 2,952
  • 1
  • 30
  • 49
2

Surprised no one mentioned the 'ol tried and true DirEnv to auto setup environment for your current working directory. That way env is private to your local machine and available to all your scripts including inside the code base. It comes in handy for things that won't look at a .env file by default.

I use this all over my machine not just in development projects.

Def worth checking out! Direnv

jbool24
  • 566
  • 4
  • 3