how to get rsync to recognise ssh publickey when run from script (nodejs)

Strange issue here.

Running a script calling rsync over ssh throws

"Permission denied (publickey). rsync: connection unexpectedly closed (0 bytes received so far) [receiver]"

strangely:

  1. ssh user@host works, so ssh keys are all set up correctly
  2. logging and pasting the command being spawned by the script into the terminal works
  3. running whoami from script shows correct user, and environment is passed to spawn instance via process.env
  4. running ssh user@host from script does not throw error and returns buffer, so that seems to work
  5. running the script (which simply execs the already tested command) throws the above error

    var spawn = require('child_process').spawn;
    spawn('rsync', [
        '-avc',
        '--delete',
        '"'+src+'"' , 
        '--link-dest="'+path.join(dest, folder_name)+'"',
        '"'+path.join(dest, 'latest/')+'"'
        ],{ 
            cwd: process.cwd(),
            env: process.env
    });
    

Try to specify the path to the key

rsync -avz -e "ssh -i /home/thisuser/cron/thishost-rsync-key" remoteuser@remotehost:/remote/dir /this/dir/