crontool
Paste a schedule, read it in English, see the next runs.

Running an npm Script with Cron

cd into the project so npm finds package.json, and use the absolute npm path because cron's PATH is minimal. With nvm, source it first or use the versioned binary path, or npm run simply won't be found.

The crontab line

0 3 * * * cd /opt/app && /usr/bin/npm run backup >> /var/log/npm-job.log 2>&1

The cd is required — npm run reads scripts from the package.json in the current directory.

With nvm-installed Node

nvm's node/npm live under your home dir and aren't on cron's PATH. Source nvm first:

0 3 * * * . ~/.nvm/nvm.sh && cd /opt/app && npm run backup >> /var/log/npm-job.log 2>&1

Find your npm path

Run which npm and paste the absolute path. With nvm it looks like ~/.nvm/versions/node/vXX/bin/npm.

FAQ

How do I run an npm script from cron?

cd into the project then call npm: 0 3 * * * cd /opt/app && /usr/bin/npm run backup. The cd is required so npm finds package.json.

Why is npm not found in my cron job?

Cron's PATH is minimal and doesn't include nvm's node. Use the absolute npm path or source ~/.nvm/nvm.sh before npm run.

Do I need to cd before npm run in cron?

Yes. npm run reads the scripts from package.json in the working directory, so cd into the project first.