Search code examples
javascriptnode.jslinuxcron

Node Curely Brace Require Breaks Cron


I have created a node script that I want to schedule with root's cron. The crontab entry is listed below.

SHELL=/bin/bash
0 1 * * * (time node /home/user/test/index.js) > /tmp/dbbackup 2>&1

I can run the exact same command without error as regular user or root. However, when executed from the scheduler, it breaks immediately with a syntax error

const {exec} = require('child_process');
      ^

SyntaxError: Unexpected token {
    at exports.runInThisContext (vm.js:53:16)
    at Module._compile (module.js:373:25)
    at Object.Module._extensions..js (module.js:416:10)
    at Module.load (module.js:343:32)
    at Function.Module._load (module.js:300:12)
    at Function.Module.runMain (module.js:441:10)
    at startup (node.js:140:18)
    at node.js:1043:3

I read something yesterday (that I'm having trouble linking back to now) about not running an adequate version of Node, but after updating am still seeing the same results.

 node --version
v12.19.0

What am I doing wrong here? Thanks!


Solution

  • The solution was a combination of which node and whereis node. Apparently I had v4.8.2 installed to /usr/bin/node and v12.19.0 installed to /usr/local/bin/node. After realizing this, I just specify which version I want to call in my cron.