简体   繁体   English

将值从 cmd 传递到 npm 脚本

[英]Pass values from cmd to npm script

I've searched for days but did not find an answer that worked for my problem.我已经搜索了好几天,但没有找到适合我的问题的答案。

I want to run a npm script through cmd or Powershell in Windows and pass values for script variables.我想在 ZAEA23489CE3AA9B6406EBB28E0CDA4 中通过 cmd 或 Powershell 运行npm脚本并传递脚本变量的值I would like the bellow script in package.json :我想要package.json中的波纹管脚本:

"scripts": {
    "happy-birthday": "echo Happy birthday $NAME and many returns!"
    }

To output:至 output:

Happy birthday Danny and many returns!生日快乐丹尼和许多回报!

With a command like:使用如下命令:

npm run happy-birthday --NAME=Danny

Everything I tested so far gives me:到目前为止,我测试的所有内容都给了我:

Happy birthday $NAME and many returns!生日快乐 $NAME 和许多回报!

It feels like npm does not recognize this as a variable and prints it like it is a string.感觉 npm 没有将其识别为变量并将其打印为字符串。 I also tested %NAME%.我还测试了%NAME%。

Npm version - 6.12.1 Npm 版本 - 6.12.1

You can't pass arguments to the middle of npm scripts, argument(s) can only be passed to the end of them.您不能将 arguments 传递到 npm 脚本的中间,参数只能传递到它们的末尾。 See my answer here for further explanation.在此处查看我的答案以获取更多说明。

Given your example, consider the following solution which will work successfully across all platforms:鉴于您的示例,请考虑以下解决方案,该解决方案将在所有平台上成功运行:

  1. In your package.json file define your happy-birthday npm script as follows:在您的package.json文件中定义您happy-birthday npm 脚本如下:

     "scripts": { "happy-birthday": "node -e \"console.log('Happy birthday %s and many returns,'. process.argv[1] || 'Jane')\"" }
  2. Then run the following command via cmd or Powershell (or any other command line tool).然后通过cmdPowershell (或任何其他命令行工具)运行以下命令。

     npm run happy-birthday -- Danny

    This will print:这将打印:

    Happy birthday Danny and many returns!生日快乐丹尼和许多回报!

    Note: If you just run the following command, ie without passing an argument:注意:如果您只是运行以下命令,即不传递参数:

     npm run happy-birthday

    It will print the default name instead:它将打印默认名称:

    Happy birthday Jane and many returns!简生日快乐,许多回报!


Explanation:解释:

  • The npm script utilizes the nodejs command line option -e to evaluate the inline JavaScript as follows: npm 脚本使用 nodejs 命令行选项-e来评估内联 JavaScript,如下所示:

     console.log('Happy birthday %s and many returns,'. process.argv[1] || 'Jane')
  • The arguments passed via the CLI, eg Danny , are read using process.argv - whereby we reference the Array element at index 1.通过 CLI 传递的 arguments (例如Danny )是使用process.argv读取的 - 我们在索引 1 处引用 Array 元素。

  • The Logical OR Operator , i,e. 逻辑 OR 运算符,即|| is utilized to return Jane when no argument is passed.用于在未传递任何参数时返回Jane


Edit: Setting environment variables instead编辑:改为设置环境变量

Alternatively may want to consider setting an environment variable and referencing it in your npm script.或者,可能需要考虑设置环境变量并在 npm 脚本中引用它。

  1. In your npm script define your happy-birthday npm script as follows:在您的 npm 脚本中定义您happy-birthday npm 脚本如下:

     "happy-birthday": "echo Happy birthday %NAME% and many returns!"

    Note the %NAME% notation used by Windows only to reference the variable.请注意Windows 使用的%NAME%表示法仅用于引用变量。

  2. Using cmd使用 cmd

    When using cmd (ie Command Prompt ) you'll need to run the following command:使用 cmd (即命令提示符)时,您需要运行以下命令:

     set NAME=Danny&& npm run happy-birthday

    Using Powershell使用 Powershell

    When using Powershell you'll need to run the following command instead:使用Powershell时,您需要运行以下命令:

     $env:NAME="Danny"; npm run happy-birthday

Note: The default shell that npm utilizes for npm scripts is sh on *nix and cmd on windows. Note: The default shell that npm utilizes for npm scripts is sh on *nix and cmd on windows. Therefore the aforementioned methods defined in steps 1 and 2 will fail on *nix platforms.因此,在步骤 1 和 2 中定义的上述方法将在*nix平台上失败。

If cross-platform support is requirement and you do want to take this approach of setting environment variables and referencing them via npm scripts, then consider utilizing the cross-env package.如果需要跨平台支持,并且您确实希望采用这种设置环境变量的方法并通过 npm 脚本引用它们,那么请考虑使用环境 package。

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

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