Impossible to run script file/command in Nest.js => Error: ENOENT

65 Views Asked by At

I am trying to run a script file that can take dynamic variables in Nest.js (and on windows) but no matter what I am trying to do it is not working.

Here is an example of what I'm trying to achieve:

import { spawn } from 'child_process';

 const scriptProcess = spawn('./test.sh');

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

      scriptProcess.stderr.on('data', (data) => {
        console.log(`stderr printed: ${data}`);
      });

      scriptProcess.on('error', (err) => console.log(`error: ${err}`));

and Here is my test script file which is located in the same directory of where it is called

#!/bin/bash

NAME
YEARS

echo  $NAME is $YEARS old today!

I have two types of errors depending if I set shell: true in the spawn options.

If shell: true

stderr printed: '.' is not recognized as an internal or external command

If no options

error: Error: spawn ./test.sh ENOENT

Please not that I have the same errors if I put a simple bash script in the spawn, for example ls -lh instead of ./test.sh.

In a magnificent world I would like to put the file a the source folder structure in a script folder and call it with the correct path, but one step at a time...

0

There are 0 best solutions below