'The token '&&' is not a valid statement separator in this version
On the way of installing Webpack on my React Project, the following problem hinders my progress:
last step to configure the Webpack
npm run build && node ./dist/main.js
Error on Windows Power Shell / on Visual Studio Code
PS C:\Users\pythonbuddha\Desktop\to_experiment\to-do-list> npm run build && node ./dist/main.js
At line:1 char:15
+ npm run build && node ./dist/main.js
+ ~~
The token '&&' is not a valid statement separator in this version.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : InvalidEndOfLine
Tutorial which promised to configure the webpack
https://developerhandbook.com/webpack/webpack-4-from-absolute-scratch/
https://developerhandbook.com/webpack/how-to-configure-scss-modules-for-webpack/
Solution 1:[1]
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,
Solution 2:[2]
I found that within PowerShell as my terminal within VS Code
, replacing &&
in the command with ;
did the trick.
Solution 3:[3]
PowerShell (Core) v7+ - but not Windows PowerShell - now does support &&
and ||
, the pipeline-chain operators, so your command should work as-is there - see this answer for PowerShell-specific considerations for their use; see below for Windows PowerShell workarounds.
Conceptual note:
In all shells that support it (notably
cmd.exe
and POSIX-compatible shells such as Bash),&&
conditionally sequences commands: it executes its RHS command only if the LHS command indicated success;||
is the inverse: it executes the RHS only if the LHS indicated failure.This is important for preventing execution when it makes no sense to do so; e.g., in
npm run build && node ./dist/main.js
it only makes sense to run what was just built (withnode
) if the build succeeded, which is what&&
ensures.
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 plague PowerShell (Core) up to version 7.1
Solution 4:[4]
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 build
node ./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
Solution 5:[5]
I have solved the issue by upgrading powershell and renamed dir. path folder name by removing spaces. Now it works properly.
Powershell upgradation link
https://github.com/PowerShell/PowerShell/releases/tag/v7.2.0-preview.8
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tibebes. M |
Solution 2 | Toni |
Solution 3 | |
Solution 4 | JensV |
Solution 5 | ameen trivandrum |