Search code examples
javascriptnode.jschild-process

Go up a directory to run a python script in parent directory, using Node Js Child Process


I am having issues figuring out how I can go up a directory from my server running to run a python script. I am able to run a python script using the child process, {spawn}, however, I am unable to go up once, to the parent directory to run the python script.

The code I currently have works only if the file is in the current direcrtory

    const b = brand+'.py'

    const childPython = spawn('python3', [b]);

    childPython.stdout.on('data', (data) =>{
        console.log(`data: ${data}`)
    })

Solution

  • I presume you're using child_process.spawn() to run your python script. To go up one directory level from where your script is, you can just pass the cwd (current working directory) option to child_process.spawn().

    child_process.spawn(command, args, {
        cwd: path.resolve(__dirname, "..")
    });