Load .env environment variables when running npm task
up vote
1
down vote
favorite
Let's say we have a .env
file with some variables specified:
AWS_PROFILE=hsz
ENVIRONMENT=development
There is also a simple npm
task defined:
"name": "project",
"version": "0.0.1",
"scripts":
"deploy": "sls deploy"
But runnning npm run deploy
ignores our .env
definition.
It can be resolved with better-npm-run
like:
"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "bnr deploy"
,
"betterScripts":
"deploy": "sls deploy"
,
"devDependencies":
"better-npm-run": "^0.1.1",
but this looks like an overhead - especially when we have 10+ tasks.
Is there a better way to always load .env
without proxying all tasks via better-npm-run
?
node.js npm environment-variables
add a comment |
up vote
1
down vote
favorite
Let's say we have a .env
file with some variables specified:
AWS_PROFILE=hsz
ENVIRONMENT=development
There is also a simple npm
task defined:
"name": "project",
"version": "0.0.1",
"scripts":
"deploy": "sls deploy"
But runnning npm run deploy
ignores our .env
definition.
It can be resolved with better-npm-run
like:
"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "bnr deploy"
,
"betterScripts":
"deploy": "sls deploy"
,
"devDependencies":
"better-npm-run": "^0.1.1",
but this looks like an overhead - especially when we have 10+ tasks.
Is there a better way to always load .env
without proxying all tasks via better-npm-run
?
node.js npm environment-variables
any comments/feedback regarding my answer? @hsz
– mihai
Nov 23 at 18:01
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
Let's say we have a .env
file with some variables specified:
AWS_PROFILE=hsz
ENVIRONMENT=development
There is also a simple npm
task defined:
"name": "project",
"version": "0.0.1",
"scripts":
"deploy": "sls deploy"
But runnning npm run deploy
ignores our .env
definition.
It can be resolved with better-npm-run
like:
"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "bnr deploy"
,
"betterScripts":
"deploy": "sls deploy"
,
"devDependencies":
"better-npm-run": "^0.1.1",
but this looks like an overhead - especially when we have 10+ tasks.
Is there a better way to always load .env
without proxying all tasks via better-npm-run
?
node.js npm environment-variables
Let's say we have a .env
file with some variables specified:
AWS_PROFILE=hsz
ENVIRONMENT=development
There is also a simple npm
task defined:
"name": "project",
"version": "0.0.1",
"scripts":
"deploy": "sls deploy"
But runnning npm run deploy
ignores our .env
definition.
It can be resolved with better-npm-run
like:
"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "bnr deploy"
,
"betterScripts":
"deploy": "sls deploy"
,
"devDependencies":
"better-npm-run": "^0.1.1",
but this looks like an overhead - especially when we have 10+ tasks.
Is there a better way to always load .env
without proxying all tasks via better-npm-run
?
node.js npm environment-variables
node.js npm environment-variables
asked Nov 9 at 6:48
hsz
104k48200269
104k48200269
any comments/feedback regarding my answer? @hsz
– mihai
Nov 23 at 18:01
add a comment |
any comments/feedback regarding my answer? @hsz
– mihai
Nov 23 at 18:01
any comments/feedback regarding my answer? @hsz
– mihai
Nov 23 at 18:01
any comments/feedback regarding my answer? @hsz
– mihai
Nov 23 at 18:01
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
A bit ugly, but you could try something like this:
"scripts":
"deploy": "export $(cat .env
This will export all environment variables from the .env
file before running sls deploy
.
There are some variations to this tehnique in this answer.
Not very clean but it avoids usage of an extra module.
add a comment |
up vote
0
down vote
Maintain and load all your environment specific configuration in project itself.
dev.js
module.exports =
"host":"dev.com"
prod.js
module.exports =
"host":"prod.com"
config.js - main file that will resolve configuration based on process.env.ENV variable.
const dev = require('./dev');
const prod = require('./prod');
let envObject = ;
const env = process.env.ENV || "dev";
switch(env)
case 'prod':
envObject = prod;
break;
default:
envObject = dev;
envObject['ENV'] = env;
process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
module.exports = envObject;
index.js - node project root file call every time when project start
const config = require('./config');
console.log('config object => ',config.host);
package.json
"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "sls deploy"
Running you node.js code
Prod environment ENV=prod npm run deploy
;
Development environment - npm run deploy
;
Default environment is set to dev
in ./config.js
Using this simple practice you don't need any npm module to manage your environment configurations.
Thanks, but the point is to avoid prefixingnpm run
with any env variables; also in case of runningsls deploy
- external tool - noindex.js
is loaded at all.
– hsz
Nov 9 at 8:31
Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
– front_end_dev
Nov 9 at 8:46
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
A bit ugly, but you could try something like this:
"scripts":
"deploy": "export $(cat .env
This will export all environment variables from the .env
file before running sls deploy
.
There are some variations to this tehnique in this answer.
Not very clean but it avoids usage of an extra module.
add a comment |
up vote
1
down vote
accepted
A bit ugly, but you could try something like this:
"scripts":
"deploy": "export $(cat .env
This will export all environment variables from the .env
file before running sls deploy
.
There are some variations to this tehnique in this answer.
Not very clean but it avoids usage of an extra module.
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
A bit ugly, but you could try something like this:
"scripts":
"deploy": "export $(cat .env
This will export all environment variables from the .env
file before running sls deploy
.
There are some variations to this tehnique in this answer.
Not very clean but it avoids usage of an extra module.
A bit ugly, but you could try something like this:
"scripts":
"deploy": "export $(cat .env
This will export all environment variables from the .env
file before running sls deploy
.
There are some variations to this tehnique in this answer.
Not very clean but it avoids usage of an extra module.
answered Nov 9 at 10:21
mihai
23.1k73968
23.1k73968
add a comment |
add a comment |
up vote
0
down vote
Maintain and load all your environment specific configuration in project itself.
dev.js
module.exports =
"host":"dev.com"
prod.js
module.exports =
"host":"prod.com"
config.js - main file that will resolve configuration based on process.env.ENV variable.
const dev = require('./dev');
const prod = require('./prod');
let envObject = ;
const env = process.env.ENV || "dev";
switch(env)
case 'prod':
envObject = prod;
break;
default:
envObject = dev;
envObject['ENV'] = env;
process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
module.exports = envObject;
index.js - node project root file call every time when project start
const config = require('./config');
console.log('config object => ',config.host);
package.json
"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "sls deploy"
Running you node.js code
Prod environment ENV=prod npm run deploy
;
Development environment - npm run deploy
;
Default environment is set to dev
in ./config.js
Using this simple practice you don't need any npm module to manage your environment configurations.
Thanks, but the point is to avoid prefixingnpm run
with any env variables; also in case of runningsls deploy
- external tool - noindex.js
is loaded at all.
– hsz
Nov 9 at 8:31
Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
– front_end_dev
Nov 9 at 8:46
add a comment |
up vote
0
down vote
Maintain and load all your environment specific configuration in project itself.
dev.js
module.exports =
"host":"dev.com"
prod.js
module.exports =
"host":"prod.com"
config.js - main file that will resolve configuration based on process.env.ENV variable.
const dev = require('./dev');
const prod = require('./prod');
let envObject = ;
const env = process.env.ENV || "dev";
switch(env)
case 'prod':
envObject = prod;
break;
default:
envObject = dev;
envObject['ENV'] = env;
process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
module.exports = envObject;
index.js - node project root file call every time when project start
const config = require('./config');
console.log('config object => ',config.host);
package.json
"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "sls deploy"
Running you node.js code
Prod environment ENV=prod npm run deploy
;
Development environment - npm run deploy
;
Default environment is set to dev
in ./config.js
Using this simple practice you don't need any npm module to manage your environment configurations.
Thanks, but the point is to avoid prefixingnpm run
with any env variables; also in case of runningsls deploy
- external tool - noindex.js
is loaded at all.
– hsz
Nov 9 at 8:31
Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
– front_end_dev
Nov 9 at 8:46
add a comment |
up vote
0
down vote
up vote
0
down vote
Maintain and load all your environment specific configuration in project itself.
dev.js
module.exports =
"host":"dev.com"
prod.js
module.exports =
"host":"prod.com"
config.js - main file that will resolve configuration based on process.env.ENV variable.
const dev = require('./dev');
const prod = require('./prod');
let envObject = ;
const env = process.env.ENV || "dev";
switch(env)
case 'prod':
envObject = prod;
break;
default:
envObject = dev;
envObject['ENV'] = env;
process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
module.exports = envObject;
index.js - node project root file call every time when project start
const config = require('./config');
console.log('config object => ',config.host);
package.json
"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "sls deploy"
Running you node.js code
Prod environment ENV=prod npm run deploy
;
Development environment - npm run deploy
;
Default environment is set to dev
in ./config.js
Using this simple practice you don't need any npm module to manage your environment configurations.
Maintain and load all your environment specific configuration in project itself.
dev.js
module.exports =
"host":"dev.com"
prod.js
module.exports =
"host":"prod.com"
config.js - main file that will resolve configuration based on process.env.ENV variable.
const dev = require('./dev');
const prod = require('./prod');
let envObject = ;
const env = process.env.ENV || "dev";
switch(env)
case 'prod':
envObject = prod;
break;
default:
envObject = dev;
envObject['ENV'] = env;
process.env = Object.assign(process.env,envObject); // Optional if you prefer to add them into process environment otherwise `require('./config')` where you need configuration.
module.exports = envObject;
index.js - node project root file call every time when project start
const config = require('./config');
console.log('config object => ',config.host);
package.json
"name": "project",
"version": "0.0.2",
"scripts":
"deploy": "sls deploy"
Running you node.js code
Prod environment ENV=prod npm run deploy
;
Development environment - npm run deploy
;
Default environment is set to dev
in ./config.js
Using this simple practice you don't need any npm module to manage your environment configurations.
answered Nov 9 at 8:17
front_end_dev
1,3151511
1,3151511
Thanks, but the point is to avoid prefixingnpm run
with any env variables; also in case of runningsls deploy
- external tool - noindex.js
is loaded at all.
– hsz
Nov 9 at 8:31
Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
– front_end_dev
Nov 9 at 8:46
add a comment |
Thanks, but the point is to avoid prefixingnpm run
with any env variables; also in case of runningsls deploy
- external tool - noindex.js
is loaded at all.
– hsz
Nov 9 at 8:31
Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
– front_end_dev
Nov 9 at 8:46
Thanks, but the point is to avoid prefixing
npm run
with any env variables; also in case of running sls deploy
- external tool - no index.js
is loaded at all.– hsz
Nov 9 at 8:31
Thanks, but the point is to avoid prefixing
npm run
with any env variables; also in case of running sls deploy
- external tool - no index.js
is loaded at all.– hsz
Nov 9 at 8:31
Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
– front_end_dev
Nov 9 at 8:46
Or create a .npmrc file inside the project root folder and set variable there. docs.npmjs.com/files/npmrc#files
– front_end_dev
Nov 9 at 8:46
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53221045%2fload-env-environment-variables-when-running-npm-task%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
any comments/feedback regarding my answer? @hsz
– mihai
Nov 23 at 18:01