2

I have a file (.env) of environment variables defined at the root of my NPM module which looks like this:

FOO=bar
BAZ=zoo

I would like to have these variables made available when I run the module's package.json scripts.

Here's an attempt from my package.json:

{
  ...
  "scripts": {
    "foo": "source ./.env && echo $FOO"
  }
  ...
}

But, when I run npm run foo I get this error:

sh: 1: source: not found

How can I make $FOO available in this context?

A cross-OS solution would best, but for now I'm running on Ubuntu.

2
  • Do you use dotenv? Commented Apr 2, 2019 at 19:55
  • 1
    I saw dotenv, but didn't see a way for it work directly embedded in the scripts section. In my case, I'm not trying to run a node.js program...just a bash script Commented Apr 2, 2019 at 19:56

1 Answer 1

1

I don't think NPM has a built-in for that, but you can prefix all your scripts with a minimal export command:

{
  "scripts": {
    "foo": "export $(cat .env | xargs) && echo $FOO"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.