Reputation: 762
I'd like inside my package.json execute some script which will set environment variables that my app could use. I need something like this (in my package.json)
"scripts": {
"dev": "set-env-vars.sh && webpack-dev-server --hot --inline & cd server && NODE_ENV=development node app.js",
},
This is my set-env-vars.sh file:
#!/bin/bash
export ENV_MONGODB_URI="mongodb://localhost:27017/mydb"
...
I know that I need use source or . to export my variables into shell, this is working in shell, but not for my package.json dev-script.
Any solutions to achieve that? Thank you.
Upvotes: 0
Views: 505
Reputation: 1594
The problem is in single ampersand (&) after webpack-dev-server invocation. Trailing ampersand directs the shell to run the command in the background. In practice, your command will be split into 2 parts, which are going to be invoked in separate shells asynchronously. If you use ampersand (&) intentionally, then you should source env vars in both parts like this:
. ./set-env-vars.sh && webpack-dev-server --hot --inline & . ./set-env-vars.sh && cd server && NODE_ENV=development node app.js
so, variables from set-env-vars.sh will be available for both webpack-dev-server and node
Or if you want to run it synchronously:
. ./set-env-vars.sh && webpack-dev-server --hot --inline && cd server && NODE_ENV=development node app.js
Upvotes: 1