简体   繁体   English

如何使用 Powershell 从 package.json 脚本设置变量?

[英]How to set a variable from package.json script using Powershell?

The problem I am currently having is that we have a script in our package.json set to run single test files using the following syntax:我目前遇到的问题是我们在 package.json 中有一个脚本设置为使用以下语法运行单个测试文件:

"singleTest": "mocha tests/$MATCH"

However, when I attempt to run this in Windows 10 with Powershell I use the following command and get the error shown:但是,当我尝试使用 Powershell 在 Windows 10 中运行它时,我使用以下命令并得到显示的错误:

$env:MATCH="01HOME-01FC-T01.js" && npm run singleTest

Error: No test files found: "tests/$MATCH"
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! selenium-js-example@2.0.0 singleTest: `mocha tests/$MATCH`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the selenium-js-example@2.0.0 singleTest script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

It seems as if it is not setting the file name to the variable MATCH at all.似乎它根本没有将文件名设置为变量 MATCH。 I would really appreciate if someone can shed some light on what am I doing wrong or what would be the proper way to run this using Powershell 7.1.如果有人能阐明我做错了什么或使用 Powershell 7.1 运行它的正确方法,我将不胜感激。

You can chain commands in Powershell by using semicolon.您可以使用分号链接 Powershell 中的命令。 You cannot use && like in bash.您不能在 bash 中使用 &&。

$env:MATCH="01HOME-01FC-T01.js"; npm run singleTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM