I am trying to execute a command in Node:
cd "/www/foo/" && "/path/to/git/bin/git.exe" config --list --global
Basically, I want to execute a Git command that returns global configuration (for the current user).
This works fine for me when I execute the command directly on cli. It also works if I do it like this in Node:
var exec = require('child_process').exec;
var config = {
maxBuffer: 10000 * 1024
};
exec('cd "/www/foo/" && "/path/to/git/bin/git.exe" config --list --global', config, function() {
console.log(arguments);
});
However, as soon as I specify the environment to the config:
var exec = require('child_process').exec;
var config = {
maxBuffer: 10000 * 1024,
env: { // <--- this one
}
};
exec('cd "/www/foo/" && "/path/to/git/bin/git.exe" config --list --global', config, function() {
console.log(arguments);
});
Things break:
Command failed: fatal: unable to read config file
'(null)/(null)/.gitconfig': No such file or directory
I know the reason, it is because the executed Git program is no longer executed under the user environment (like it is in cli), and can't retrieve the user directory to read global configuration (in Git, global config = user config) and I believe /(null)/(null)/.gitconfig should be /Users/MyName/.gitconfig which does exist.
My question is, how can I specify the current user environment while still being able to specify my own additional environment properties?
I solved it.
I retrieved the current environment from process.env and extended it with my own properties:
var environment = process.env;
environment.customProp = 'foo';
var config = {
maxBuffer: 10000 * 1024,
env: environment
};
So the problem was I missed the entire environment when I overwrote it.
Related
So I have install npm config module.
I have trying to set a local variable that stores a secret/privateKey.
Tried setting an environment variable/private key using the following command
export VIDLY_JWTPRIVATEKEY=MYSECUREKEY
however it don't think It is being set as I get an error via
console.log("FATAL ERROR: JWTPRIVATEKEY is not defined");
This is how I am checking if the key is set..
index.js
if (!config.get("JWTPRIVATEKEY")) {
console.log("FATAL ERROR: JWTPRIVATEKEY is not defined");
// node environment variable. 1 (any other number exc. 0) is exit the app, 0 is success
process.exit(1);
}
userAuth.js I once again try to get the private, however its not set (?)
#code above
const token = jwt.sign({ _id: user._id }, config.get("JWTPRIVATEKEY"));
default.json (in config module folder)
{
"JWTPRIVATEKEY": ""
}
custom-environment-variables.json (in config folder)
{
"JWTPRIVATEKEY": "VIDLY_JWTPRIVATEKEY"
}
**
ERROR: "FATAL ERROR: JWTPRIVATEKEY is not defined"
**
What am I doing wrong?
Instead of setting the variable with this way, you can try to set it in production and development config folders like this:
config/development.js
const config = {
env: 'development',
JWTPRIVATEKEY: <your_dev_key>
};
module.exports = config;
config/production.js
const config = {
env: 'production',
JWTPRIVATEKEY: <your_prod_key>
};
module.exports = config;
it is two years later! but i think it can be useful, atleast for others.
if you set environment variable AND run the app in IDE's Terminal, it will not work!
but if do these in "cmd", it works.
I've created node application which I can run locally and in the cloud
Now I want that it be done somehow smoother and cleaner ,so I try to put some property in config.json file to check if I want to deploy the app or use it locally but I need to update manually this property before I change the propose , there is a better way to do it with node ?
let runnerServer = `http://localhost:8060/service/runner/${server.address().port}`;
if (cfg.isHosted) {
blogServer = `http://${serverName}/service/runner/${server.address().port}`;
}
and in the conig.json I've the field isHosted which I change manually(true/false) if I want to deploy or not...
update
maybe I can use process.env.PORT but this is just one example that I need to use in my code , currently I've several of fork that need to konw if Im in deployment or running locally ..
One option is to use use node's in built object called process.env (https://nodejs.org/api/process.html) and use two config files per se. This approach is somewhat similar to what you are doing but may be cleaner
config.localhost.json
config.production.json
then by setting properties on this object based on environment such as process.env.NODE_ENV = 'localhost' or process.env.NODE_ENV = 'production', you could read the corresponding file to import the configurations.
var config = require('./config.production.json');
if(process.env.NODE_ENV === 'localhost')
{
config = require('./config.localhost.json');
}
So to set this environment variable when running locally on your dev box , if
OSX - then on terminal export NODE_ENV=localhost
WINDOWS - then on cmd line set NODE_ENV=localhost
An easy way to solve this, if every environment configuration can be in the repo:
config.json:
production: {
// prod config
},
staging: {
// staging config
},
devel: {
// devel config
}
config.js:
const environment = process.env['ENV'] || 'devel';
module.exports = require('./config.json')[environment];
then in your package.json you could add the following scripts:
package.json
// npm stuff
scripts {
prod: "ENV=production node index.js",
stage: "ENV=staging node index.js",
dev: "ENV=devel node index.js"
}
and with this setup, you can run each configuration with the following commands:
production: npm run prod
staging: npm run stage
devel: npm run dev
Right now with my ui tests using WebdriverIO, I have this in my configuration file:
var baseUrl = 'http://localhost:3000';
global.testParameters = {
baseUrl: baseUrl
};
This gives me access to my base url in the tests however it has to be fixed in the configuration file and I can't use the --baseUrl option when running wdio command. The reason for this is because from everything I have read, I don't see a way to have access to command line option values in my tests.
Is there a way to access the value of the command line options (specifically --baseUrl) in my actual test files?
You can use the yargs library. Do npm install yargs and in your config file add:
var argv = require('yargs').argv;
var baseUrl = argv.baseUrl;
You can then pass in the baseUrl with --baseUrl <your URL>
You can also make use of the WebdriverIO spec (wdio.conf.js) for configuration and create a separate conf.js file for each baseUrl you'd like to run your tests against
You can pass your base location through command line using -baseUrl= like below
wdio --baseUrl=http://[device IP]
You can pass any argument you need using the same way and can access it in wdio.config.js 'onprepare' event like below
wdio --type=XXX
Then in wdio config
onPrepare: function(config, capabilities) {
if (process.argv !== undefined && process.argv.length) {
process.argv.forEach(arg => {
if (arg.indexOf('--type=') !== -1) {
process.env.type = arg.replace('--type=', '');
}
});
}
},
before: function(capabilities, specs) {
global.type = process.env.type;
}
Now your type is available through-out your workspace as a global variable.
I'd like to provide an easy and simple Docker container for an open source application that takes an URL of a configuration file as an argument and uses this file.
The Dockerfile is pretty straight forward:
FROM phusion/baseimage
# Use baseimage-docker's init system.
CMD ["/sbin/my_init"]
RUN curl -sL https://deb.nodesource.com/setup_4.x | sudo -E bash -
RUN apt-get update
RUN apt-get install -y nodejs git
ADD . /src
RUN cd /src; npm install; npm update
ENV NODE_ENV production
CMD ["/usr/bin/node", "/src/gitevents.js"]
I found no way of adding the file when the container runs (with ADD or ENTRYPOINT), so I'm trying to work it out in node.js:
docker run -e "CONFIG_URL=https://gist.githubusercontent.com/PatrickHeneise/c97ba221495df0cd9a3b/raw/fda1b8cd53874735349c6310a6643e6fc589a404/gitevents_config.js" gitevents
this sets CONFIG_URL as a environment variable that I can use in node. However, I need to download a file then, which is async, which kind of doesn't work in the current setup.
if (process.env.NODE_ENV === 'production') {
var exists = fs.accessSync(path.join(__dirname, 'common', 'production.js'), fs.R_OK);
if (exists) {
config = require('./production');
} else {
// https download, but then `config` is undefined when running the app the first time.
}
}
There's no synchronous download in node.js, any recommendations how I could solve this?
I'd love to have Docker do the job with ADD or CMD doing a curl download, but I'm not sure how that works?
Another thing would be to consider that your "config file" is not a file but just text and pass the content to the container at runtime.
CONFIG="$(curl -sL https://gist.githubusercontent.com/PatrickHeneise/c97ba221495df0cd9a3b/raw/fda1b8cd53874735349c6310a6643e6fc589a404/gitevents_config.js)"
docker run -e "CONFIG_URL=${CONFIG}" gitevents
How about a combination of ENTRYPOINT and environment variable? You'd have ENTRYPOINT in the Dockerfile set to a shell script that would download the configuration file specified in the environment variable and then start the application.
Since the entry point script would receive whatever is in CMD as it's arguments, the application start step could be accomplished by something like
# Execute CMD.
eval "$#"
I managed to re-write my config script to work asynchronous, still not the best solution in my eyes.
var config = {};
var https = require('https');
var fs = require('fs');
var path = require('path');
config.load = function(fn) {
if (process.env.NODE_ENV === 'production') {
fs.access(path.join(__dirname, 'production.js'), fs.R_OK, function(error, exists) {
if (exists) {
config = require('./production');
} else {
var file = fs.createWriteStream(path.join(__dirname, 'production.js'));
var url = process.env.CONFIG_URL;
if (!url) {
process.exit(-1);
} else {
https.get(url, function(response) {
response.pipe(file);
file.on('finish', function() {
file.close(function() {
return fn(require('./production'));
});
});
});
}
}
});
} else if (process.env.NODE_ENV === 'test') {
return fn(require('./test'));
} else {
return fn(require('./development'));
}
};
module.exports = exports = config;
hi am new to nodejs environment.
am using nodeJs + compoundjs.
am having three database environment development. production and test. my question in when i run the NODE_ENV=production node . command, all url's,port number and other things should get from production.js. when i shift the node environment by giving command NODE_ENV=development node . all things need to run should get from development.js.
any notes for this also helpful for me.
if anybody has any idea please share with me.
You have to set the Environment and then you can configure your app like:
(This is a mongoose db and express, but you can find similar configurations.)
Simply set up three environment configurations
app.configure('development', function () {
mongoose.connect(devConfig.db.path, function onMongooseError(err) {
});
});
app.configure('production', function () {
mongoose.connect(proConfig.db.path, function onMongooseError(err) {
});
});
a configuration example (config.js) :
var config = {};
// Database (MongoDB) configurations
config.db = {
path : 'mongodb://localhost/sampleDatabase'
};
module.exports = config;
I require this file in my app.js by var config = require('config')
You could do the Environment detection in the config file as well.