Skip to main content

I am attempting to run npm installs via a script from self service. However there are nothing but errors, these installs work fine if run from terminal.

Have tried the following:

su -l $currentuser -c npm install aws-cdk-lib su $currentuser -c npm install aws-cdk-lib sudo -H -iu $currentuser npm install aws-cdk-lib

These all return an error of "Script result: zsh:1: command not found: npm"

Picking apart a brew install script I have, I noticed it referenced an exec file for the "brew" portion of the install command located in the /opt/homebrew/bin/brew folder. There is no such npm folder, so I had to improvise and found a similarly named exec file.

Trying to replicate this I created the following:

npm=/Users/${currentuser}/.nvm/versions/node/v21.1.0/lib/node_modules/npm/bin/npm cd ~ sudo -H -iu ${currentuser} ${npm} install aws-cdk-lib

This returns the most educated error of "Script result: Could not determine Node.js install directory"


I feel like I'm getting closer, but am hitting a wall with my scripting knowledge and these package managers.

Any information here would be appreciated! Thank you!

Not sure if it’s too late to answer this, but I found out that you need to use a combination of Homebrew, then use brew install nvm (i have not figured out how to automate this), node version, for example nvm install node 24 and then you can leverage NPM via scripts within Jamf Pro.

 

 

But prior to that you need to set that “environmental variable” in the .bash_profile, this can also be also be scripted and turned into a policy. 

Setting up profile for NVM 

#!/bin/bash

# Define the NVM configuration lines
nvm_config="""
export NVM_DIR=\"\$HOME/.nvm\"
[ -s \"\$NVM_DIR/nvm.sh\" ] && \\. \"\$NVM_DIR/nvm.sh\" # This loads nvm
[ -s \"\$NVM_DIR/bash_completion\" ] && \\. \"\$NVM_DIR/bash_completion\" # This loads nvm bash_completion
"""

# Get the current logged-in user
current_user=$(stat -f "%Su" /dev/console)

# Define the path to the user's .bash_profile
bash_profile_path="/Users/$current_user/.bash_profile"

# Check if .bash_profile exists, create it if not
if [ ! -f "$bash_profile_path" ]; then
    touch "$bash_profile_path"
    chown "$current_user":staff "$bash_profile_path"
fi

# Check if NVM configuration already exists in .bash_profile
if ! grep -q "export NVM_DIR" "$bash_profile_path"; then
    echo "$nvm_config" >> "$bash_profile_path"
    echo "NVM configuration added to $bash_profile_path for user $current_user."
else
    echo "NVM configuration already exists in $bash_profile_path for user $current_user. No changes made."
fi

exit 0

 

Using NVM and NPM 

#!/bin/bash

    # Define NVM_DIR (adjust if your NVM installation path is different)
    export NVM_DIR="$HOME/.nvm"

    # Source nvm.sh to load nvm functions
    [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

    # Now you can use nvm commands
    nvm install 22 
    
    # Example: use Node.js version 16
    # npm install # Example: run npm install after setting the Node version