【发布时间】:2020-06-19 18:16:01
【问题描述】:
我使用以下 shell 命令来激活我的提示。在 shell 中工作正常。
source <(curl -s https://gist.githubusercontent.com/jbnv/78ce47382faf454553ef/raw/4382bf0a2ac3742dfd777e34fbb14b16b0f3d06b/prompt.sh)
我把它放到一个 NPM 脚本中,这样我就可以在多个项目之间共享它,而不必完全输入它:
"scripts": {
"prompt": "source <(curl -s https://gist.githubusercontent.com/jbnv/78ce47382faf454553ef/raw/4382bf0a2ac3742dfd777e34fbb14b16b0f3d06b/prompt.sh)"
},
当我npm run script 时,它会抛出sh: 1: Syntax error: "(" unexpected。
我该如何解决这个问题?
【问题讨论】:
-
Npm 使用
sh作为执行 npm 脚本的默认 shell - 而不是bash。如here 所述,sh不支持进程替换,即<()。要么尝试; A) 将您的脚本作为字符串传递给bash -c,例如将您的 npm 脚本定义为;"prompt": "bash -c \"source <(curl -s https://gist.githubusercontent.com/jbnv/78ce47382faf454553ef/raw/4382bf0a2ac3742dfd777e34fbb14b16b0f3d06b/prompt.sh)\"" -
B) 或者尝试使用npm-config 命令将script-shell 设置改为
bash。您可以运行which bash来获取 bash 的路径,然后运行类似的东西;npm config set script-shell "/bin/bash"- 上述示例中的/bin/bash部分应替换为从which bash返回的路径 -
A 和 B 都不起作用,看起来原因是 NPM 在自己的 shell 中运行命令。因此,提示在命令后消失的 shell 中发生了变化。如果是这种情况,则没有解决方案,因为该命令永远不会按预期工作。
标签: npm npm-scripts