Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to consume environment variables inside of npm / package.json?

I'm attempting to build a package.json so that when running a NodeJS app on Heroku it will run the scripts.postinstall step using an environment variable. For example:

...
"scripts": {
  "postinstall": "command $ENV_VAR"}
},
...

I've looked at the docs and wasn't able to find something saying I can.

Is this even possible? Is this even desirable and "I'm Doing It Wrong"™?

like image 860
Rob Fagen Avatar asked Oct 06 '15 21:10

Rob Fagen


2 Answers

Updated answer due to new packages having been written

You can use the cross-var package to do this in a clean way:

...
"scripts": {
  ...
  "postinstall": "cross-var command $ENV_VAR",
  ...
},
"dependencies": {
  ...
  "cross-var": "^1.1.0",
  ...
}
...

Original answer

To answer the last questions, because they're the most important one: yes, no, and absolutely, because you've just broken cross-platform compatibility. There is no guarantee your environment syntax works for all shells on all operating systems, so don't do this.

We have a guaranteed cross-platform technology available to us already: Node. So, create a file called something like bootstrap.js, and then make npm run node bootstrap as your postinstall script. Since the code inside bootstrap.js will run like any other node script, it'll have access to process.env in a fully cross-platform compatible way, and everyone will be happy.

And many, many, many things that use common utils have node equivalents, so you can npm install them, locally rather than globally, and then call them in an npm script. For instance mkdir -p is not cross-platform, but installing the mkdirp module is, and then an npm script like "ensuredirs": "mkdirp dist/assets" works fine everywhere when run as npm run ensuredirs

And for convenience, the most common unix utilities have their own runner package, shx, which is fully cross-platform and makes the lives of devs even easier, with the "if you're writing code" equivalent being fs-extra.

like image 79
Mike 'Pomax' Kamermans Avatar answered Sep 20 '22 15:09

Mike 'Pomax' Kamermans


Ignore the nay-sayers. You can do this in a cross-platform manner using cross-var:

"scripts": {
    "postinstall": "cross-var command $ENV_VAR"
}
like image 25
Mark Woon Avatar answered Sep 18 '22 15:09

Mark Woon