The token '&&' is not a valid statement separator in this version The token '&&' is not a valid statement separator in this version powershell powershell

The token '&&' is not a valid statement separator in this version


It's because you're in PowerShell, try running it in CMD or Git Bash

Alternatively (if you wish to continue in PS):

(npm run build) -and (node ./dist/main.js)

3rd Alternative, just run them separetly,


The && operator is used in linux bash to run both commands after each other. (Also if the first command fails, the second won't be executed)

This does not work in PowerShell on Windows so just split both commands and run them separately:

npm run buildnode ./dist/main.js

For completeness, Powershell can behave the same when you do (command1) -and (command2) and && might actually work depending on your PowerShell version.

See this for further info: https://stackoverflow.com/a/564092/2232127


PowerShell (Core) v7+ - but not Windows PowerShell - now does support && and ||, the pipeline-chain operators, so your command should work as-is there.

&& executes its RHS only if the LHS indicated success, analogous to how && works in POSIX-compatible shells such as bash and in cmd.exe on Windows.

Windows PowerShell workarounds:

The most succinct workaround:

npm run build; if ($?) { node ./dist/main.js }

This builds on the automatic $? variable, which is a Boolean indicating whether the most recent command succeeded.

The most robust workaround, needed if the commands use 2> redirections:

npm run build; if ($LASTEXITCODE -eq 0) { node ./dist/main.js }

Basing the success test on the automatic $LastExitCode variable, which reflects the process exit code of the most recently executed external program, avoids problems in Windows PowerShell[1] where the presence of stderr output in combination with redirecting it via 2> mistakenly sets $? to $false even when the process exit code is 0.


[1] The problems with 2> redirections are summarized in this answer. They also still plague PowerShell (Core) as of version 7.1, but the preview versions of v7.2 now have an experimental feature, PSNotApplyErrorActionToStderr, that corrects them.