简体   繁体   English

将环境变量从命令行传递到纱线

[英]Pass environment variable from command line to yarn

I have a code that reads port number from environment variable or from config.我有一个代码可以从环境变量或配置中读取端口号。 Code looks like this代码看起来像这样

const port = process.env.PORT || serverConfig.port;
await app.listen(port);

To run app without defining environment variable, I run following yarn command.要在不定义环境变量的情况下运行应用程序,我运行以下 yarn 命令。

yarn start:dev

This command works successfully in Linux shell and Windows command line.此命令在 Linux shell 和 Windows 命令行中成功运行。

Now, I want to pass environment variable.现在,我想传递环境变量。 I tried following,我试着跟随,

PORT=2344 yarn start:dev

This commands works successfully in Linux shell but failing in Windows command line.此命令在 Linux shell 中成功运行,但在 Windows 命令行中失败。 I tried following ways but couldn't get it to work.我尝试了以下方法,但无法正常工作。

Tried: PORT=2344 yarn start:dev试过:PORT=2344 yarn start:dev

I got error: 'PORT' is not recognized as an internal or external command, operable program or batch file.我收到错误:'PORT' 不是内部或外部命令,也不是可运行的程序或批处理文件。

Tried: yarn PORT=2344 start:dev试过:纱线 PORT=2344 开始:开发

I got error: yarn run v1.17.3 error Command "PORT=2344" not found.我收到错误:yarn run v1.17.3 error Command "PORT=2344" not found。 info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.信息 访问https://yarnpkg.com/en/docs/cli/run获取有关此命令的文档。

Any idea please?有什么想法吗? I know, I can define environment variables from System Properties in Windows. But any way if I can do it from command line?我知道,我可以从 Windows 中的系统属性定义环境变量。但是如果我可以从命令行执行它,有什么办法吗?

i'd suggest you use the NPM module called cross-env .我建议你使用名为cross-env的 NPM 模块。 it allows adding particular env variables on the command line regardless of platform.无论平台如何,它都允许在命令行上添加特定的环境变量。 with that said, you may try:话虽如此,您可以尝试:

$ cross-env PORT=2344 yarn start:dev

You can chain commands on the Windows command prompt with & (or && ).您可以在 Windows 命令提示符下使用& (或&& )链接命令。 To set a environment variable you need to use the set command.要设置环境变量,您需要使用set命令。
The result should look like this: set PORT=1234 && yarn start:dev .结果应如下所示: set PORT=1234 && yarn start:dev

HTH, GL, HF :) HTH、GL、HF :)

Found a solution for this problem in Windows command prompt.在 Windows 命令提示符中找到了解决此问题的方法。

  1. Create a .env file in project root folder (outside src folder).在项目根文件夹(src 文件夹外)中创建一个 .env 文件。

  2. Define PORT in it.在其中定义 PORT。 In my case, contents of .env file will be,在我的情况下, .env 文件的内容将是,

 PORT=2344

  1. Run yarn start:dev运行纱线开始:开发

  2. Application will use port number that you have specified in .env file.应用程序将使用您在 .env 文件中指定的端口号。

You can use popular package dotenv:您可以使用流行的包 dotenv:

create a file .env in root directory put all your env vars在根目录中创建一个文件 .env 放置所有环境变量

eg:例如:

ENV=DEVELOPMENT

run your code like this像这样运行你的代码

$ node -r dotenv/config your_script.js

here the explanation:这里的解释:

[https://github.com/motdotla/dotenv#preload] [https://github.com/motdotla/dotenv#preload]

Put .env file at root..env文件放在根目录下。 Then following command will expose content of .env file and then run yarn start command然后以下命令将公开.env文件的内容,然后运行yarn start command

$ source .env && yarn start

or this command或者这个命令

$ export $(cat .env) && yarn start

If update any variable in .env then close the terminal and open new terminal window and can again run above command.如果更新 .env 中的任何变量,则关闭终端并打开新的终端窗口,然后可以再次运行上述命令。 Or else can also run unset command to remove existing var.或者也可以运行 unset 命令来删除现有的 var。

unset VAR_NAME

To define environment variables in the Windows command prompt we can use the set command, you can then split your call into two lines.要在 Windows 命令提示符中定义环境变量,我们可以使用 set 命令,然后您可以将调用拆分为两行。

set PORT=2344
yarn start:dev

The set command persists within the current command prompt, so you only need to run it once. set 命令保留在当前命令提示符下,因此您只需运行一次。

The equivalent command in bash is 'export'. bash 中的等效命令是“导出”。

FYI (not a direct answer).仅供参考(不是直接回答)。 I was attempting this in VS Code - passing.env variables through yarn to a JavaScript app.我在 VS Code 中尝试这样做 - 通过 yarn 将 .env 变量传递给 JavaScript 应用程序。 Google had very few examples so I'm sharing this for posterity as it's somewhat related.谷歌的例子很少,所以我把它分享给后代,因为它有些相关。

The following simply substitutes text normally placed directly into the package.json or script file.以下内容只是简单地替换了通常直接放入 package.json 或脚本文件中的文本。 Use this to quickly obfuscate or externalize your delivery configurations.使用它可以快速混淆或外部化您的交付配置。

In Environment Variable File (.env)在环境变量文件 (.env) 中

PORT=2344

In Yarn File (package.json)在纱线文件 (package.json)

source .env; yarn ./start.sh --port $PORT

In Yarn Script (start.sh)在纱线脚本中(start.sh)

#!/bin/bash 
while [ $? != 0 ]; do
    node dist/src/index.js $1;  #replace with your app call#
done

The app then accepts port as a variable.然后应用程序接受端口作为变量。 Great for multi-tenant deployments.非常适合多租户部署。

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

相关问题 从操作系统级别配置中删除环境变量的命令行 - Command line to remove an environment variable from the OS level configuration Windows 命令行:不评估环境变量 - Windows Command Line: Non Evaluation of Environment Variable 从命令行列出所有环境变量 - List all environment variables from the command line 如何从命令提示符下访问环境变量? - How to access environment variable from command prompt? 为什么 for 循环中的单个 % 在命令行中没有被解释为环境变量,而是在批处理文件中? - Why is a single % in a for loop not interpreted as an environment variable at the command line but it is in a batch file? 如何在Windows命令行中的环境变量中读取文件的内容? - how to read content of a file in a environment variable in windows command line? 在一行中设置环境变量和执行命令的最佳方法 - Best way to set environment variable and execute command in one line 通过命令行添加系统路径环境变量? - Adding a system path environment variable via command line? 为什么Windows的“路径”环境变量和命令行不同? - Why are “Path” Windows's environment variable and the command line different? Windows命令行:为什么环境变量在&之后不可用 - Windows command line: why environment variable is not available after &
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM