Cannot import modules in node script if it is running from cron

I have a module underscore index set globally with npm number. if i run the script

/usr/local/bin/node /home/olmo/project/processDrivesMultiUser.js

it will work fine, no matter where I am on the way, but if I run cronjob as follows:

3,18,33,48, * * * * /usr/local/bin/node /home/olmo/project/processDrivesMultiUser.js

I get this error:

Date: Wed, 10 Sep 2014 16:26:01 -0600
From: Cron Daemon <root@db.local>
To: olmo@db.local
Subject: Cron <olmo@db> /usr/local/bin/node /home/olmo/project/processDrivesMultiUser.js


module.js:340
    throw err;
          ^
Error: Cannot find module 'underscore'
    at Function.Module._resolveFilename (module.js:338:15)
    at Function.Module._load (module.js:280:25)
    at Module.require (module.js:364:17)
    at require (module.js:380:17)
    at Object.<anonymous> (/home/olmo/sandbox/api_ievwebapp/parseAdminScripts/processDrivesMultiUser.js:20:9)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)

somehow it cannot find the module underscoreif it works through cron. I made a cron entry for the same username that I use to run the script manually.

any ideas?

+4
source share
1 answer

I could find the answer based on mu's comment too short.

I modified crontab to include the NODE_PATH environment variable

3,18,33,48, * * * * export NODE_PATH=/usr/local/lib/node_modules/ && /usr/local/bin/node /home/olmo/project/processDrivesMultiUser.js

node.js, cron.

+4

All Articles