最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to exec in NodeJS using the user's environment? - Stack Overflow

programmeradmin6浏览0评论

I am trying to execute a mand in Node:

cd "/www/foo/" && "/path/to/git/bin/git.exe" config --list --global

Basically, I want to execute a Git mand that returns global configuration (for the current user).

This works fine for me when I execute the mand 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 am trying to execute a mand in Node:

cd "/www/foo/" && "/path/to/git/bin/git.exe" config --list --global

Basically, I want to execute a Git mand that returns global configuration (for the current user).

This works fine for me when I execute the mand 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?

Share Improve this question edited Nov 5, 2011 at 14:55 Tower asked Nov 5, 2011 at 14:49 TowerTower 103k131 gold badges364 silver badges521 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 15

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.

发布评论

评论列表(0)

  1. 暂无评论