Run npm with QProcess under MacOs(osx)
-
I try to execute a npm command from my software. For that I call a Process. But I receive as error that /bin/sh: npm: command not found.
I am under MacOS (Catalina).
void AtTerminal::lunch() { if(QSysInfo::productType() == "windows"){ m_process->setProgram("cmd"); m_process -> setArguments( QStringList() << "/C" << m_command ); }else if (QSysInfo::productType() == "osx") { m_process->setProgram("sh"); m_process -> setArguments( QStringList() << "-c" << m_command); }else { m_process->setProgram("sh"); m_process -> setArguments( QStringList() << "-c" << m_command ); } m_process->startDetached(); m_process->close(); }
The process can't recognize the npm command although Nodejs is well installed.
Please, could someone help me. -
@Dannick-Stark What happens if you open a terminal and try to execute npm?
You probably need to use absolute path to npm executable. -
Probably the system path that your program has started with, and that
sh
inherits, does not contain the commandnpm
.
You can check the default environment by looking atQProcessEnvironment::systemEnvironment().toStringList()
.
You can use the absolute path tonpm
or insert a suitable PATH through QProcess::setProcessEnvironment(). -
@Dannick-Stark What is content of m_command? And what is its type?
-
@ChrisW67 I looked at what was in QProcessEnvironment::systemEnvironment().toStringList(). And indeed npm was not there.
So I added it with:AtTerminal::AtTerminal() { QProcessEnvironment env = QProcessEnvironment::systemEnvironment(); env.insert("npm", "/usr/local/bin/npm"); env.insert("pnpm", "/usr/local/bin/pnpm"); m_process->setProcessEnvironment(env); }
I add the PATH in the constructor of my class. But it had no effect.
@jsulm in m_command there ist: npm install i also try to use /usr/local/bin/npm install
-
@Dannick-Stark said in Run npm with QProcess under MacOs(osx):
in m_command there ist: npm install i also try to use /usr/local/bin/npm install
As one string? QProcess parameters need to be a QStringList, all of them:
QStringList m_command << "npm" << "install";
That's why I asked what the type is.
-
@Dannick-Stark Please pass it as QStringList. It is also described in QProcess documentation.
-
In my code above, you can see that I pass the command to a QStringList.
void AtTerminal::lunch() { if(QSysInfo::productType() == "windows"){ m_process->setProgram("cmd"); m_process -> setArguments( QStringList() << "/C" << m_command ); }else if (QSysInfo::productType() == "osx") { m_process->setProgram("sh"); m_process -> setArguments( QStringList() << "-c" << m_command); }else { m_process->setProgram("sh"); m_process -> setArguments( QStringList() << "-c" << m_command ); } m_process->startDetached(); m_process->close(); }
-
@Dannick-Stark Again: ALL parameters should be passed as QStringList.
You said that m_command is a QString containing "npm install", so it basically contains TWO parameters!
It should be like this:m_process -> setArguments( QStringList() << "-c" << "npm" << "install");
Also, do you really need to start a shell? Can't you call npm directly without sh?
-
If it is possible to run npm directly without using sh that would be great.
But so far I haven't found a way to do it. -
@Dannick-Stark said in Run npm with QProcess under MacOs(osx):
But so far I haven't found a way to do it.
Simply use npm as command to execute:
m_process->setProgram("npm"); m_process -> setArguments( QStringList() << "install");
-
@Dannick-Stark One correction to what I wrote before (thanks @JonB for the hint): if you use "sh -c" then on Linux you have to pass everything after -c as ONE string!