Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Run npm with QProcess under MacOs(osx)
Forum Updated to NodeBB v4.3 + New Features

Run npm with QProcess under MacOs(osx)

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 895 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dannick Stark
    wrote on last edited by
    #1

    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.

    jsulmJ 1 Reply Last reply
    0
    • D Dannick Stark

      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.

      jsulmJ Online
      jsulmJ Online
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Dannick-Stark What happens if you open a terminal and try to execute npm?
      You probably need to use absolute path to npm executable.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      D 1 Reply Last reply
      1
      • C Offline
        C Offline
        ChrisW67
        wrote on last edited by
        #3

        Probably the system path that your program has started with, and that sh inherits, does not contain the command npm.
        You can check the default environment by looking at QProcessEnvironment::systemEnvironment().toStringList().
        You can use the absolute path to npm or insert a suitable PATH through QProcess::setProcessEnvironment().

        D 1 Reply Last reply
        0
        • jsulmJ jsulm

          @Dannick-Stark What happens if you open a terminal and try to execute npm?
          You probably need to use absolute path to npm executable.

          D Offline
          D Offline
          Dannick Stark
          wrote on last edited by
          #4

          @jsulm I try it. With this, the Process is able to recognize npm but not the commands that npm will call.

          I get as error: node command not found

          jsulmJ 1 Reply Last reply
          0
          • D Dannick Stark

            @jsulm I try it. With this, the Process is able to recognize npm but not the commands that npm will call.

            I get as error: node command not found

            jsulmJ Online
            jsulmJ Online
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @Dannick-Stark What is content of m_command? And what is its type?

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • C ChrisW67

              Probably the system path that your program has started with, and that sh inherits, does not contain the command npm.
              You can check the default environment by looking at QProcessEnvironment::systemEnvironment().toStringList().
              You can use the absolute path to npm or insert a suitable PATH through QProcess::setProcessEnvironment().

              D Offline
              D Offline
              Dannick Stark
              wrote on last edited by Dannick Stark
              #6

              @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

              jsulmJ 1 Reply Last reply
              0
              • D Dannick Stark

                @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

                jsulmJ Online
                jsulmJ Online
                jsulm
                Lifetime Qt Champion
                wrote on last edited by
                #7

                @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.

                https://forum.qt.io/topic/113070/qt-code-of-conduct

                D 1 Reply Last reply
                0
                • jsulmJ jsulm

                  @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.

                  D Offline
                  D Offline
                  Dannick Stark
                  wrote on last edited by Dannick Stark
                  #8

                  @jsulm yes as one string.
                  I pass the command to A QStringList

                  m_process -> setArguments( QStringList() << "-c" << m_command);
                  

                  It works well on Windows.

                  jsulmJ 1 Reply Last reply
                  0
                  • D Dannick Stark

                    @jsulm yes as one string.
                    I pass the command to A QStringList

                    m_process -> setArguments( QStringList() << "-c" << m_command);
                    

                    It works well on Windows.

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on last edited by
                    #9

                    @Dannick-Stark Please pass it as QStringList. It is also described in QProcess documentation.

                    https://forum.qt.io/topic/113070/qt-code-of-conduct

                    1 Reply Last reply
                    0
                    • D Offline
                      D Offline
                      Dannick Stark
                      wrote on last edited by Dannick Stark
                      #10

                      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();
                      }
                      
                      jsulmJ 1 Reply Last reply
                      0
                      • D Dannick Stark

                        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();
                        }
                        
                        jsulmJ Online
                        jsulmJ Online
                        jsulm
                        Lifetime Qt Champion
                        wrote on last edited by jsulm
                        #11

                        @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?

                        https://forum.qt.io/topic/113070/qt-code-of-conduct

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          Dannick Stark
                          wrote on last edited by
                          #12

                          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.

                          jsulmJ 2 Replies Last reply
                          0
                          • D Dannick Stark

                            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.

                            jsulmJ Online
                            jsulmJ Online
                            jsulm
                            Lifetime Qt Champion
                            wrote on last edited by
                            #13

                            @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");
                            

                            https://forum.qt.io/topic/113070/qt-code-of-conduct

                            1 Reply Last reply
                            2
                            • D Dannick Stark

                              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.

                              jsulmJ Online
                              jsulmJ Online
                              jsulm
                              Lifetime Qt Champion
                              wrote on last edited by
                              #14

                              @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!

                              https://forum.qt.io/topic/113070/qt-code-of-conduct

                              1 Reply Last reply
                              1

                              • Login

                              • Login or register to search.
                              • First post
                                Last post
                              0
                              • Categories
                              • Recent
                              • Tags
                              • Popular
                              • Users
                              • Groups
                              • Search
                              • Get Qt Extensions
                              • Unsolved