node.jsmacosapplescriptnpmnode-supervisor

MAC OS - Node.js (NPM) - Installing via AppleScript with Admin Rights


When I run the following commands one by one in Terminal it works and installs,

sudo npm install supervisor


sudo npm install forever

It asks for the admin password in Terminal window and installs fine.

In AppleScript I run this as,

tell application "Terminal"
    do script "sudo npm install supervisor" in window 1
end tell
tell application "Terminal"
    do script "sudo npm install forever --global" in window 1
end tell

It opens Terminal and asks the password and waits for user response to enter the password to continue. I tried the following AppleScript,

do shell script "sudo npm install supervisor" with administrator privileges
do shell script "sudo npm install forever --global" with administrator privileges

And got the following error,

error "sudo: npm: command not found" number 1

The AppleScript needs to ask for the password once in the common enter the username and password dialog and run the,

sudo npm install supervisor


sudo npm install forever

In Terminal without asking for password in Terminal window. How to do it?


Solution

  • https://developer.apple.com/library/mac/technotes/tn2065/_index.html

    Shell scripts do not by default have your path exported into them with apple script. In addition, they are run in shell instead of your default Terminal shell (most likely bash). You should include the full path to npm (/usr/local/bin/npm for me; find using which npm) instead of just npm when trying to run an apple script.

    However, when you specify the full path to npm, you run into another problem. npm can't find node in the path. The solution I found to work was to export PATH in the apple script.

    export PATH=$PATH:/usr/local/bin; sudo npm install forever

    Double check that /usr/local/bin contains both node and npm. This should allow you to successfully install without being prompted.