npm scripts: read .env file
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?
npm npm-scripts
add a comment |
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?
npm npm-scripts
add a comment |
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?
npm npm-scripts
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?
npm npm-scripts
npm npm-scripts
edited Jan 2 at 19:47
RobC
6,51392539
6,51392539
asked Jan 1 at 20:18
David VielhuberDavid Vielhuber
543717
543717
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Short answer: There's no terse way to achieve this, as per your second example which references the $npm_package_config
variable.
Instead, 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:
- Utilize the dotenv package to load the
.env
file. - Execute the command using the nodejs built-in module;
child_process.exec()
orchild_process.execSync()
.
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 utilizeprocess.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"
}
Thanks RobC for your detailled help. I just published a package for this problem, have a look at github.com/vielhuber/from-env
– David Vielhuber
Jan 16 at 21:20
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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%2f53998640%2fnpm-scripts-read-env-file%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Short answer: There's no terse way to achieve this, as per your second example which references the $npm_package_config
variable.
Instead, 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:
- Utilize the dotenv package to load the
.env
file. - Execute the command using the nodejs built-in module;
child_process.exec()
orchild_process.execSync()
.
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 utilizeprocess.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"
}
Thanks RobC for your detailled help. I just published a package for this problem, have a look at github.com/vielhuber/from-env
– David Vielhuber
Jan 16 at 21:20
add a comment |
Short answer: There's no terse way to achieve this, as per your second example which references the $npm_package_config
variable.
Instead, 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:
- Utilize the dotenv package to load the
.env
file. - Execute the command using the nodejs built-in module;
child_process.exec()
orchild_process.execSync()
.
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 utilizeprocess.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"
}
Thanks RobC for your detailled help. I just published a package for this problem, have a look at github.com/vielhuber/from-env
– David Vielhuber
Jan 16 at 21:20
add a comment |
Short answer: There's no terse way to achieve this, as per your second example which references the $npm_package_config
variable.
Instead, 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:
- Utilize the dotenv package to load the
.env
file. - Execute the command using the nodejs built-in module;
child_process.exec()
orchild_process.execSync()
.
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 utilizeprocess.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"
}
Short answer: There's no terse way to achieve this, as per your second example which references the $npm_package_config
variable.
Instead, 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:
- Utilize the dotenv package to load the
.env
file. - Execute the command using the nodejs built-in module;
child_process.exec()
orchild_process.execSync()
.
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 utilizeprocess.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"
}
answered Jan 2 at 14:24
RobCRobC
6,51392539
6,51392539
Thanks RobC for your detailled help. I just published a package for this problem, have a look at github.com/vielhuber/from-env
– David Vielhuber
Jan 16 at 21:20
add a comment |
Thanks RobC for your detailled help. I just published a package for this problem, have a look at github.com/vielhuber/from-env
– David Vielhuber
Jan 16 at 21:20
Thanks RobC for your detailled help. I just published a package for this problem, have a look at github.com/vielhuber/from-env
– David Vielhuber
Jan 16 at 21:20
Thanks RobC for your detailled help. I just published a package for this problem, have a look at github.com/vielhuber/from-env
– David Vielhuber
Jan 16 at 21:20
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.
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%2f53998640%2fnpm-scripts-read-env-file%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