令牌'&&'在此版本中不是有效的语句分隔符



在我的React项目上安装Webpack的过程中,以下问题阻碍了我的进度:

配置Webpack的最后一步

npm run build && node ./dist/main.js

Windows Power Shell/Visual Studio代码上的错误

PS C:UserspythonbuddhaDesktopto_experimentto-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

承诺配置webpack的教程

https://developerhandbook.com/webpack/webpack-4-from-absolute-scratch/
https://developerhandbook.com/webpack/how-to-configure-scss-modules-for-webpack/

这是因为您在PowerShell中,请尝试在CMDGit Bash中运行它

或者(如果您希望在PS中继续(:

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

第三种选择,只需单独运行它们,

我发现在PowerShell中,作为我在VS Code中的终端,用;替换命令中的&&起到了作用。

PowerShell(Core(v7+-但不是Windows PowerShell-现在确实支持管道链运算符&&||,所以您的命令应该按原样运行-有关PowerShell使用时的特定注意事项,请参阅此答案;请参阅下面的Windows PowerShell解决方法。

概念说明:

  • 在所有支持它的shell(尤其是cmd.exe和POSIX兼容shell,如Bash(中,&&有条件地排序命令:只有当LHS命令指示success时,它才会执行RHS命令;||是相反的:只有当LHS指示故障时,它才会执行RHS。

  • 这对于在毫无意义的情况下防止执行非常重要;例如,在
    npm run build && node ./dist/main.js中,只有当构建成功时,才有意义运行刚刚构建的内容(使用node(,而这正是&&所确保的。

Windows PowerShell解决方案:

最简洁的解决方法:

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

这建立在自动$?变量的基础上,该变量是一个布尔值,用于指示最近的命令是否成功。

最强大的解决方法,如果命令使用2>重定向,则需要

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

成功测试基于自动$LastExitCode变量,该变量反映了最近执行的外部程序的进程退出代码,避免了Windows PowerShell[1]中的问题,即stderr输出的存在与通过2>重定向它相结合,错误地将$?设置为$false,即使进程退出代码为0


[1]在这个答案中总结了2>重定向的问题。它们还困扰PowerShell(Core(7.1版之前的

&&运算符在linuxbash中用于相继运行这两个命令。(同样,如果第一个命令失败,第二个命令将不会执行(

这在Windows上的PowerShell中不起作用,所以只需拆分两个命令并分别运行即可:

npm run build
node ./dist/main.js

为了完整性,当您执行(command1) -and (command2)时,Powershell可以执行相同的操作,而&&可能实际工作,这取决于您的Powershell版本。

请参阅此以获取更多信息:https://stackoverflow.com/a/564092/2232127

由于有些人可能会以与我相同的方式到达这里,想知道为什么PowerShell不允许我在if语句中的两个变量之间执行条件AND运算符。

我通过用If条件评估左条件,然后在第一个真条件中立即评估右条件来解决它。

产生错误的示例:

$Path = "C:\Temptest"
$RootPath = "C:\Temp"
$currentChildren = Get-ChildItem -Force -LiteralPath $Path 
$isEmpty = $currentChildren -eq null
if ($isEmpty && $Path -ne $RootPath) {
Remove-Item -Force -LiteralPath $Path
}
$foo = "question"
$bar = "answer"

我用来解决这个问题的例子:

$Path = "C:\Temptest"
$RootPath = "C:\Temp"
$currentChildren = Get-ChildItem -Force -LiteralPath $Path 
$isEmpty = $currentChildren -eq null
if ($isEmpty) {
if ($Path -ne $RootPath) {
Remove-Item -Force -LiteralPath $Path
}
}

我已经通过升级powershell并通过删除空格重命名dir.path文件夹名称来解决这个问题。现在它工作正常。

Powershell升级链接

https://github.com/PowerShell/PowerShell#get-powershell

在Windows终端上出现此问题。解决方案是使用;代替&&,如:

git fetch --all ; git add --all ; git commit -m 'updating things' ; git push

在vs代码或phpstorm的ps终端中,只需替换&与,并击出安打代替

npm install && npm run dev

进行

npm install; npm run dev

这对我有效:

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

就像cmd:中的命令一样

npm run build && node ./dist/main.js

使用Git-bash可以很好地工作。

看看吧。。。

示例

user@DevKitchenMINGW64~/OneDrive/文档/BL_proxy/公文包(主($cd src&mkdir内容&cd内容&触摸导航栏.js about.js services.js projects.js

最新更新