1

I've got a nodeserver and I'd like to start everything it depends on before launching it. The package runs on windows but depends on a multi-container docker setup in WSL.

So I've tried


  "scripts": {
    "start": "wsl 'sudo service docker start && docker-compose up -d' && nodemon server.js --config nodemon.json",
  },

but it throws

> Executing task: npm run start <

> wsl 'sudo service docker start && docker-compose up -d' && nodemon server.js --config nodemon.json

zsh:1: unmatched '
The terminal process "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command npm run start" terminated with exit code: 1.

What would be the correct syntax to do this?

1 Answer 1

2

You do have mismatched quotes in there, but I'm assuming that's just an artifact of trying a bunch of different edits ;-).

The best way to pass a script (multiple commands) into the wsl command is to pass it to sh (or bash if needed). Taking just the WSL part for now, that would be:

wsl -e sh -c "sudo service docker start && docker-compose up -d"

From the error message you are getting, it looks like this is getting parsed through powershell.exe, which I don't believe supports && chaining. If I understand correctly, that's only available in PowerShell Core (pwsh.exe).

So that would make the full command (hopefully):

wsl -e sh -c "sudo service docker start && docker-compose up -d"; nodemon server.js --config nodemon.json

Quoting shouldn't be too bad on that, so hopefully:

"scripts": {
    "start": "wsl -e sh -c 'sudo service docker start && docker-compose up -d'; nodemon server.js --config nodemon.json",
  },

Not tested directly of course, but let me know if it doesn't work for you, and I can check out what I might have gotten wrong.

If you really do need to "chain" based on the previous commands being successful, then perhaps consider something like:

wsl -e sh -c "sudo service docker start && docker-compose up -d && powershell.exe nodemon server.js --config nodemon.json"

... which again, is pretty easy to quote. But if that doesn't work (or you end up needing more than one command that runs in PowerShell), then quoting starts to get much more difficult:

wsl -e sh -c "sudo service docker start && docker-compose up -d && powershell.exe -c \`"nodemon server.js --config nodemon.json\`""
Sign up to request clarification or add additional context in comments.

5 Comments

I only want to run the docker commands in WSL. The nodemon part should be run in CMD. Hence the quote at up -d' && nodemon (...)
@TomM Ah, right. I knew part of that felt wrong, and I kept re-reading it to try to figure it out. Thanks! Will work on editing.
thank you, I forgot about escaping. Final command: "start": "wsl -e sh -c \"sudo service docker start && docker-compose up -d\"; nodemon server.js --config nodemon.json"
the solutions with single quotes for some reason always gave me an "unterminated string" error
@TomM It might be that things really are getting processed through CMD instead of PowerShell. In that case, you might be able to get around it by wrapping everything in another powershell.exe -c "". Either way, quoting/escaping is going to start becoming more painful.

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.