NodeJS Child Process EXEC Command Failed with NVM Permission denied OSX

天涯浪子 提交于 2021-02-17 05:18:07

问题


I am attempting to launch nvm within a child process in Nodejs on OSX However in doing so, I am getting the following error: /bin/sh: /Users/miker/.nvm/nvm.sh: Permission denied. child process exited with code 126 (I call the explicit path to nvm, since running without it, the child process can't see the executable.)

This is obvious that it is a permission issue. However, I am not sure why since I can launch the commands on there own without issue. It is only when launching in a child process does this fail. Perhaps, the child process runs in the context of another profile? If so, is there a way to maintain the current profile or context?

Here is an example code

let exec = require('child_process').exec;

let child = exec('echo $NVM_DIR && $NVM_DIR/nvm.sh use && npm install', {
    cwd: './build/'
});

child.stdout.on('data',
    (data) => {
        console.log(data);
    });

child.stderr.on('data',
   (data) => {
        //throw errors
        console.log(data);
    });

child.on('close', (code) => {
    console.log('child process exited with code ' + code);
});

I am using NodeJS 7.2.1 and nvm 0.32.1 If anyone has a solution to this problem, please let me know.


回答1:


~/.nvm/nvm.sh is not executable script, it is meant to be "sourced" (not run in a separate shell, but loaded and executed in the current shell context).

Trying to run it as if it were executable would result in a permission error, because it doesn't have executable permissions.

I don't know if it's going to work, but try this instead:

echo $NVM_DIR && source $NVM_DIR/nvm.sh && nvm use VERSION && npm install

You may have to explicitly set the shell option for child_process.exec() to make sure that the command line is run in a "full" shell (like /bin/bash).



来源:https://stackoverflow.com/questions/43725871/nodejs-child-process-exec-command-failed-with-nvm-permission-denied-osx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!