motdotla / dotenv

Loads environment variables from .env for nodejs projects.
https://www.dotenvx.com
BSD 2-Clause "Simplified" License
19.01k stars 853 forks source link

Use of dotenv in a cron job with ES6 modules #787

Closed NateWr closed 9 months ago

NateWr commented 9 months ago

I am struggling to find an example of using this package in a Node script that is run by a cron job. All of the examples I have found rely on __dirname (see here), which is not available when running a script as a module with the import syntax.

Running from cron seems like a core use case for Node scripts. Can anyone provide a working example of how to configure this so that Node scripts work regardless of the calling context? Or is this a problem that should be solved upstream somehow?

NateWr commented 9 months ago

It looks like the problem I'm facing is due to cron running under the root user, using an outdated version of node. Apologies for the noise.

motdotla commented 7 months ago

no problem. i've been there myself before!

in the future consider dotenvx for this kind of thing. you might find it easier to install and get using. it's designed to run anywhere. it can wrap any script or command like this:

dotenvx run -- nodex index.js

There's a cron example on the README