Home

Using NVM in cron jobs (Node Version Manager with crontabs)

If you have tried using npm or node installed with nvm (Node Version Manager) in cron jobs, you may have run into the infamous “node: command not found” error.

Bellow is the workaround I have been using lately:

crontab
$ crontab -e

* * * * /bin/bash /home/devimal/Desktop/demo.sh
demo.sh
# Load nvm
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm use 12.13.0

# now node works
node -e "console.log('hello')"
node --version

# npm works too!
npm --version

Extras

You can also make the script executable and run it ad-hoc:

Adhoc
# Make it executable
$ chmod +x /home/devimal/Desktop/demo.sh

# Run it
$ ./home/devimal/Desktop/demo.sh