Home > Software engineering >  Shell command works in terminal, but fails in node
Shell command works in terminal, but fails in node

Time:12-18

I'm having a problem with a code in typescript that executes a shell script. The code's part that executes the shell script is something like below:

const child = child_process.exec(command.script, (error: child_process.ExecException, stdout: string, stderr: string) => {
    /* ... */
});

It's working fine, but when I try to execute the following script, it fails:

{
    name: "Gerar aab",
    script: `cd ${PATH}/${projectId}/aplicativo/platforms/android && ./gradlew bundle`
}

Console result (stderr):

Project evaluation failed including an error in afterEvaluate {}. Run with --stacktrace for details of the afterEvaluate {} error.

FAILURE: Build failed with an exception.

* Where:
Script '/opt/lupi/lupiteste/aplicativo/platforms/android/CordovaLib/cordova.gradle' line: 121

* What went wrong:
A problem occurred evaluating project ':app'.
> Unable to determine Android SDK directory.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 9s

The problem is that the same command works perfectdly in terminal. I tried use shelljs and now child_process, and cwd option instead cd but the same issue. The application is running in a Debian 10 server. How to solve it, please?

CodePudding user response:

The default shell for exec is `/bin/sh'. Your shell I'm the terminal is likely bash. So that's probably one difference, but you can specify a shell in the options.

This looks like a problem with the environment of the script, so you probably want to check that the expected path environment variable is what you think it is in the child process. If you need to, you can specify the environment in the options too.

  • Related