skip to Main Content

I have simple PHP script for checking npm and npm plugin. It works well via command line, but partially with php exec.

$errors = '';
$outPut = null;
$exitCode = null;
exec('npm -v', $outPut, $exitCode); //<- this works
if($exitCode !== 0) {
    $errors = 'npm is not installed!';
    return false;
}
$outPut = null;
$exitCode = null;
exec('lessc -v', $outPut, $exitCode); //<- this works
if($exitCode !== 0) {
    $errors = 'lessc is not installed!';
    return false;
}

//check version of plugin: npm view/info less-plugin-autoprefix version
$outPut = null;
$exitCode = null;
exec('npm info less-plugin-autoprefix version', $outPut, $exitCode); //<- this not works !!!, $exitCode is 7 and $outPut is empty
if($exitCode !== 0) {
    $errors = 'lessc plugin autoprefix is not installed!';
    return false;
}

All works except command ‘npm info less-plugin-autoprefix version’. As I said in command line it will return excepted correct result.
Anything wrong with my code?

Using exec command npm info less-plugin-autoprefix version 2>&1 produce:

Array
(
    [0] => Error: Failed to replace env in config: ${APPDATA}
    [1] =>     at C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:415:13
    [2] =>     at String.replace (<anonymous>)
    [3] =>     at envReplace (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:411:12)
    [4] =>     at parseField (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:389:7)
    [5] =>     at C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:330:24
    [6] =>     at Array.forEach (<anonymous>)
    [7] =>     at Conf.add (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:328:23)
    [8] =>     at ConfigChain.addString (C:Program Filesnodejsnode_modulesnpmnode_modulesconfig-chainindex.js:244:8)
    [9] =>     at Conf.<anonymous> (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:316:10)
    [10] =>     at C:Program Filesnodejsnode_modulesnpmnode_modulesgraceful-fsgraceful-fs.js:115:16
    [11] => TypeError: Cannot read property 'loaded' of undefined
    [12] =>     at exit (C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:97:27)
    [13] =>     at errorHandler (C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:216:3)
    [14] =>     at C:Program Filesnodejsnode_modulesnpmbinnpm-cli.js:78:20
    [15] =>     at cb (C:Program Filesnodejsnode_modulesnpmlibnpm.js:225:22)
    [16] =>     at C:Program Filesnodejsnode_modulesnpmlibnpm.js:263:24
    [17] =>     at C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:81:7
    [18] =>     at Array.forEach (<anonymous>)
    [19] =>     at Conf.<anonymous> (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:80:13)
    [20] =>     at Conf.f (C:Program Filesnodejsnode_modulesnpmnode_modulesonceonce.js:25:25)
    [21] =>     at emitOne (events.js:116:13)
    [22] => openssl config failed: error:02001002:system library:fopen:No such file or directory
    [23] => C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:97
    [24] =>   var doExit = npm.config.loaded ? npm.config.get('_exit') : true
    [25] =>                           ^
    [26] => 
    [27] => TypeError: Cannot read property 'loaded' of undefined
    [28] =>     at exit (C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:97:27)
    [29] =>     at process.errorHandler (C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:216:3)
    [30] =>     at emitOne (events.js:116:13)
    [31] =>     at process.emit (events.js:211:7)
    [32] =>     at process._fatalException (bootstrap_node.js:378:26)
    [33] => Error: Failed to replace env in config: ${APPDATA}
    [34] =>     at C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:415:13
    [35] =>     at String.replace (<anonymous>)
    [36] =>     at envReplace (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:411:12)
    [37] =>     at parseField (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:389:7)
    [38] =>     at C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:330:24
    [39] =>     at Array.forEach (<anonymous>)
    [40] =>     at Conf.add (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:328:23)
    [41] =>     at ConfigChain.addString (C:Program Filesnodejsnode_modulesnpmnode_modulesconfig-chainindex.js:244:8)
    [42] =>     at Conf.<anonymous> (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:316:10)
    [43] =>     at C:Program Filesnodejsnode_modulesnpmnode_modulesgraceful-fsgraceful-fs.js:115:16
    [44] => TypeError: Cannot read property 'loaded' of undefined
    [45] =>     at exit (C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:97:27)
    [46] =>     at errorHandler (C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:216:3)
    [47] =>     at C:Program Filesnodejsnode_modulesnpmbinnpm-cli.js:78:20
    [48] =>     at cb (C:Program Filesnodejsnode_modulesnpmlibnpm.js:225:22)
    [49] =>     at C:Program Filesnodejsnode_modulesnpmlibnpm.js:263:24
    [50] =>     at C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:81:7
    [51] =>     at Array.forEach (<anonymous>)
    [52] =>     at Conf.<anonymous> (C:Program Filesnodejsnode_modulesnpmlibconfigcore.js:80:13)
    [53] =>     at Conf.f (C:Program Filesnodejsnode_modulesnpmnode_modulesonceonce.js:25:25)
    [54] =>     at emitOne (events.js:116:13)
    [55] => openssl config failed: error:02001002:system library:fopen:No such file or directory
    [56] => C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:97
    [57] =>   var doExit = npm.config.loaded ? npm.config.get('_exit') : true
    [58] =>                           ^
    [59] => 
    [60] => TypeError: Cannot read property 'loaded' of undefined
    [61] =>     at exit (C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:97:27)
    [62] =>     at process.errorHandler (C:Program Filesnodejsnode_modulesnpmlibutilserror-handler.js:216:3)
    [63] =>     at emitOne (events.js:116:13)
    [64] =>     at process.emit (events.js:211:7)
    [65] =>     at process._fatalException (bootstrap_node.js:378:26)
)

Is it possible to fix it programatically?
I dont know if its some problems but in command line 'npm info less-plugin-autoprefix version' works well without errors.

2

Answers


  1. Chosen as BEST ANSWER

    After some experiments I got working solution. Problem is that Windows platform work different.

    $c = get_defined_constants();
    if(isset($c['PHP_OS']) && (strtoupper($c['PHP_OS']) === 'WINNT' || strtoupper($c['PHP_OS']) === 'WIN32')) {
        define('OS', 'WIN');
    }
    else {
        define('OS', 'LIN');
    }
    function array_search_partial($arr, $keyword) {
        foreach($arr as $index => $string) {
            if (strpos($string, $keyword) !== FALSE)
                return $string;
        }
    }
    
    $outPut = null;
    $exitCode = null;
    $appdata = getenv('APPDATA');
    if(!$appdata) {
        if(OS === 'WIN') {
            $paths = $_SERVER['PATH'];
            if($paths) {
                //C:Usersmy_user_nameAppDataRoamingnpm
                $userPath = array_search_partial(explode(';', $paths), 'AppDataRoamingnpm');
                $userPath = mb_substr($userPath, 0, -4);
                putenv("APPDATA=$userPath");
            }
        }
        else {
    
        }
    
    }
    

    then command:

    exec('npm info less-plugin-autoprefix version', $outPut, $exitCode);
    

    works as expected.


  2. Redirect the error output to the standard output :

    npm info less-plugin-autoprefix version 2>&1
    

    This way, the errors will be available in the $outPut array too

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search