1

I created an rpm package that extract a Node.js project into a specific folder and then I created a postinstall script that cd into that folder and perform an "npm install --production"

#!/bin/bash
cd /opt/project
npm install --production

The problem is that after rpm unpacks the project I receive the fallowing error:

/var/tmp/rpm-tmp.MbfpY5: line 3: npm: command not found
warning: %post(pack-0.0.0-1.noarch) scriptlet failed, exit status 127

Of course npm is installed and, if I execute the same script manually, it works. I read that the exit status 127 is also related to a bug in selinux policies, but I tried to disable selinux completely and received the same error.

I also have the exact same code as a .deb and its working flawless there, but I can't make it work in CentOS.

Any idea where the problem might be?

Thank you

  • what is the result of which npm, at first sight, I would say npm is not in your PATH. – Archemar Mar 3 '15 at 9:32
  • @Archemar yes it's definitely in the PATH, the result of which is "/usr/local/bin/npm" also keep in mind that if I execute the same script manually it works, that's why I can't make sense of this... – Cado Mar 3 '15 at 9:42
  • are you sure /usr/local/bin is in PATH during rpm's postinstall phase ? – Archemar Mar 3 '15 at 9:50
  • Do you mind explaining a bit better what you mean? I'm not sure I follow, sorry. how can the PATH be different in the post phase install? – Cado Mar 3 '15 at 10:16
  • PATH can be reset by installer to avoid security risks. (If I am normal user in your system, I might create a bogus mv or cp , located in /usr/local/bin, which can modify system if run by root) – Archemar Mar 3 '15 at 10:25
1

First you should try to focus on the error

npm not found

You say that when you execute the script, it works - I suppose that it is this one:

#!/bin/bash
cd /opt/project
npm install --production 
  • did you run it as a normal user or as root?
  • when you install the rpm I guess that it is as root.
  • have you tried to put the absolute path of npm in the script (whereis npm)

If the last one works, the problem is that npm is not in the PATH variable of root. You should try to check it (as root):

#echo $PATH

Edit:

So the $PATH variable is set with the good paths and when you use the absolute path for npm it works. That means that the shell doesn't read the PATH variable in order to find the executable (in all the example of scriptlet I have found, they always use fullpath. Try to see if you can use command expansion with which:

#!/bin/bash
cd /opt/project
$(which npm) install --production
  • I installed and launched the script from root in both cases, npm is in the PATH of the root user. Launching from the full path will probably work, but it's not really a viable option since the installation path of npm may be different in different systems. I installed npm downloading the binaries from the nodejs website and issuing the command: "tar --strip-components 1 -xzvf node-v* -C /usr/local" The only thing that makes sense to me is what @Archemar said, that during the install process the PATH is reset/limited for security reason, but I haven't found a solution yet. – Cado Mar 4 '15 at 12:19
  • Yes I know that a fullpath in the script is not a viable solution but you should try in order to know if it is really an issue related to the PATH variable. – cedlemo Mar 4 '15 at 12:49
  • Just tried and everything is working if I give him the command with full path: "/usr/local/bin/npm install --production" – Cado Mar 4 '15 at 13:07
  • Fun thing is, if I call "echo $PATH" inside that script, it prints exactly the root PATH, included the /usr/local/bin. I'm really lost here... – Cado Mar 4 '15 at 13:16
  • I have updated my answer. Maybe we should search in the bash options or something like that but make a try with which – cedlemo Mar 4 '15 at 15:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.