Mitya
Mitya

Reputation: 34566

Series of node commands hangs on npx command

I have a node script whose job is to scaffold an app I've built. It creates files and folders then runs a series of node commands. All works perfectly. These are the commands that are run:

const postInstCmds = [
    'npm init -y',
    'npm install http-server --save-dev',
    'npx add-npm-scripts server "http-server"',
    'npm run server'
];
postInstCmds.forEach(cmd => execSync(cmd, {cwd: appDir}));
console.log('Done!');

However, in the terminal, it seems to hang on the penultimate (npx) command, even though it does run the final npm run server command (because the created app is available on localhost:8080). For presumably the same reason I never see my "Done!" log. This is what I see:

enter image description here

How can I get the terminal to complete the commands without hanging on the npx one?

Upvotes: 2

Views: 639

Answers (1)

Trott
Trott

Reputation: 70085

You never see "Done" because the last command never exits. The penultimate command does in fact exit. We know this because otherwise the last command wouldn't run (since you are using execSync() and that will block until the command exits) and you would not have your server running.

What appears to be happening is that you are only seeing STDERR and not STDOUT. Without seeing the rest of your code, I can't say exactly why that is happening, but a potential easy fix could be to specify that all stdio should be inherited.

postInstCmds.forEach(cmd => execSync(cmd, {cwd: appDir, stdio: 'inherit'}));

Upvotes: 3

Related Questions