TKoL
TKoL

Reputation: 13902

How can I make sure my child Node process is starting from the correct directory?

I run Node.js processes in windows server. Some npm packages require that the node process starts from the correct directory, eg config -- I ran into this problem initially because I made a Task Manager task to run a .bat file that started the node process, but if I didn't explicitly set the TaskManager task to start from the containing folder of my package.json, config wouldn't find the correct files to load.

So if I'm starting a node process from within another node process, through something like require('child_process').fork, do I still have to make sure it's being called from a specific directory? How do I do that?

Upvotes: 2

Views: 155

Answers (1)

ralphtheninja
ralphtheninja

Reputation: 133028

If you do .fork(), .exec() or .spawn() on child_process, they all take an options object which in turn has a .cwd property. For example

const cp = require('child_process')
const child = cp.spawn('./whatever', { cwd: '/path/to/dir' })

Upvotes: 1

Related Questions