【问题标题】:source. env command does not work when called from package.json scripts in Ubuntu资源。从 Ubuntu 中的 package.json 脚本调用时,env 命令不起作用
【发布时间】:2020-10-23 14:35:07
【问题描述】:

我的 package.json 中有以下脚本键/属性条目

"stop": "source .env && yarn doc:stop && lsof -t -i tcp:$EXPRESS_PORT | xargs kill 2> /dev/null && pgrep -f $(pwd) | xargs kill 2> /dev/null",

当我执行时它不起作用

yarn stop

它会抛出以下错误

/bin/sh: 1: source: not found

但我可以直接从 Ubuntu 命令终端调用 execute 而不会出错

source .env

我在使用 . (点)而不是来源。

/bin/sh: 1: .: .env: not found

我该如何解决?

谢谢

【问题讨论】:

    标签: ubuntu yarnpkg


    【解决方案1】:

    我认为有两种选择:

    1. 在您的停止脚本定义中使用 bash:

    "stop" : "bash -ac ' LOCATION="."; source .env; echo $REACT_APP_ENV; unset IFS' && ...etc"

    1. 在您的.env 添加导出,查看更多here

    第三种也是最后一种选择是您的文件不存在,您可以使用ls -la | grep .env进行验证

    【讨论】:

    • 我确定该文件存在。我测试了您的选项 1,但它不起作用。
    • @JuanPabloFernandez 我知道了,我有一个使用 cat 和 cypress 的示例,你可以试试吗? "cy:open": "export $(cat .env | xargs) && CYPRESS_CLIENT=${REACT_APP_KC_ENV} npx cypress open",
    • 我得到了以下输出 yarn run v1.21.1 $ export $(cat .env | xargs) && CYPRESS_CLIENT=${REACT_APP_KC_ENV} npx cypress open xargs: unmatched single quote;默认情况下,引号对 xargs 是特殊的,除非您使用 -0 选项 /bin/sh: 1: export: #: bad variable name error 命令失败,退出代码为 2. info 请访问 yarnpkg.com/en/docs/cli/run 获取有关此命令的文档。
    【解决方案2】:

    【讨论】:

      猜你喜欢
      • 2014-04-18
      • 2021-07-24
      • 1970-01-01
      • 1970-01-01
      • 2015-10-12
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      • 1970-01-01
      相关资源
      最近更新 更多