How to install nodejs and npm packages with salt on debian

node.jsnpmsaltstack

I'm struggling to install a recent nodejs version and npm packages using salt on debian. Here is my salt state:

nodejs-deps:
  pkg.installed:
    - names:
      - g++
      - curl
      - libssl-dev
      - apache2-utils
  require:
    - pkg: git

nodejs-source:
  git.latest:
    - target: /usr/src/nodejs
    - name: git://github.com/joyent/node.git
    - rev: v0.10.32-release

nodejs-install:
  cmd.run:
    - cwd: /usr/src/nodejs
    - name: ./configure && make && make install  
    - onlyif: if [ -z $(node --version) ] || [ $(node --version) != "v0.10.32" ]; then echo "should update"; else exit 1; fi;
    - require: 
      - git: nodejs-source
      - pkg: nodejs-deps


less:
  cmd.run:
    - name: npm install -g less

With this, node is installed but I get this error /bin/sh: 1: npm: not found for the command: npm install -g less.
Also, if I launch this state another time, the onlyif condition doesn't detect that node is installed (while it is).

I seen that there is a salt state to install npm packages but I couldn't have it to work either. I guess it's because I'm not installing it from the package manager (since I can't find packages for node and npm).

Best Answer

Thanks to viq on the IRC channel, I was able to solve this problem. The solution is to change the $PATH variable of the minion /etc/init.d/salt-minion:

Change this:

PATH=/sbin:/usr/sbin:/bin:/usr/bin

by

PATH=/sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin

You may have to call service salt-minion restart on the minion.

Related Topic