babel-node from cron job not working

400 views Asked by At

I'm currently unable to run babel-node from a cron job. My code runs as expected when I manually run it, but the same code won't run from cron - here's my cron file:

* * * * * cd /path/to/dir && /path/to/dir/node_modules/.bin/babel-node --presets es2015 /path/to/dir/babelScript.js >> ~/babelScript.log

babelScript.log is completely empty. What's more, similar code runs correctly when using node (not babel-node):

* * * * * cd /path/to/dir && /path/to/.nvm/versions/node/v6.11.2/bin/node /path/to/dir/nodeScript.js >> ~/nodeScript.log

In this instance the expected output is printed to nodeScript.log. I'm completely baffled, so would really appreciate any help!

1

There are 1 answers

0
user2097278 On

I still haven't managed to get this code to run from a cron script, but it's actually not recommended to use babel-node in production anyway.

For anyone else in a similar situation, I compiled the script using babel first like so:

babel babelScript.js --out-file babelScript-compiled.js

Then you're able to run the compiled script from cron like any other node script:

* * * * * cd /path/to/dir && /path/to/.nvm/versions/node/v6.11.2/bin/node /path/to/dir/babelScript-compiled.js

More info in the babel docs.