1

I want to use spawn to execute like

  cd ../ && ls

I tried

const { spawn } = require('child_process');
spawn('cd', ['../', '&&', 'ls'], {
    stdio: 'inherit',
});

But failed with:

Error: spawn cd ENOENT
    at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
    at onErrorNT (internal/child_process.js:415:16)
    at process._tickCallback (internal/process/next_tick.js:63:19)
    at Function.Module.runMain (internal/modules/cjs/loader.js:834:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)
Emitted 'error' event at:
    at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
    at onErrorNT (internal/child_process.js:415:16)
    [... lines matching original stack trace ...]
    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)

I've googled for this problem for a while, but got no good idea, please help me...

1 Answer 1

1

&& is a shell operator. In order to use it, you'll need to specify shell: true when spawning, and have the entire "chain" as a command, not as arguments:

const { spawn } = require('child_process');
spawn('cd .. && ls', {
    shell: true,
    stdio: 'inherit',
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.