简体   繁体   English

NodeJS 子进程 EXEC 命令失败,NVM 权限被拒绝 OSX

[英]NodeJS Child Process EXEC Command Failed with NVM Permission denied OSX

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.我试图在 OSX 上的 Nodejs 的子进程中启动 nvm 但是这样做时,我收到以下错误:/bin/sh:/Users/miker/.nvm/nvm.sh:权限被拒绝。 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.)子进程以代码 126 退出(我调用 nvm 的显式路径,因为没有它运行,子进程看不到可执行文件。)

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.我正在使用 NodeJS 7.2.1 和 nvm 0.32.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). ~/.nvm/nvm.sh不是可执行脚本,它是“源”的(不是在单独的shell中运行,而是在当前shell上下文中加载和执行)。

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 ). 您可能必须为child_process.exec()显式设置shell选项,以确保命令行在“完整” shell(例如/bin/bash )中运行。

I had the same problem, but it wasn't related to the code, but to the IDE terminal I was using.我遇到了同样的问题,但这与代码无关,而是与我使用的 IDE 终端有关。

The solution was to change the directory in the IDE settings, when changing it was solved.解决方案是在IDE设置中更改目录,更改时解决了。

In my case I was using Jetbrains Phpstorm.就我而言,我使用的是 Jetbrains Phpstorm。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM