npm binary + windows git bash + environment variables

时间:2022-01-09 23:02:56

In my node module I've created a tool that is installed globally using npm i -g. This tool is a bash script that sets up my local configuration, calls a few other scripts, and also needs to set a few environment variables. On Linux, it works fine calling

在我的节点模块中,我创建了一个使用npm i -g全局安装的工具。此工具是一个bash脚本,用于设置本地配置,调用其他一些脚本,还需要设置一些环境变量。在Linux上,它可以很好地调用

$ . my-setup 

or

要么

$ source my-setup

This will invoke my-setup, and since calling with "dot" or source, the environment variables set by the script are exported to the calling shell. No problems.

这将调用my-setup,并且由于使用“dot”或source调用,脚本设置的环境变量将导出到调用shell。没问题。

However, when running the same script on Git bash in Windows 7, the script is invoked, but afterwards the entire shell is closed. When looking into how NPM treats globally installed binaries in Windows, I noticed

但是,在Windows 7中的Git bash上运行相同的脚本时,将调用该脚本,但之后将关闭整个shell。在研究NPM如何处理Windows中全局安装的二进制文件时,我注意到了

$ type my-setup
my-setup is /c/Users/jhh/AppData/Roaming/npm/my-setup

which in turn expands to

反过来扩展到

"$basedir/node_modules/<my-module>/my-setup.sh"   "$@"
exit $?

So, I suspect the final exit statement here is what's causing the calling shell to be closed when sourcing this script on Windows. If I manually do

因此,我怀疑这里的最终退出声明是什么导致在Windows上获取此脚本时关闭调用shell。如果我手动做

$ . "$basedir/node_modules/<my-module>/my-setup.sh"

Then the script is executed fine and variables exported properly, while not closing the shell.

然后脚本执行正常并且变量导出正确,而不关闭shell。

What's going on here? What is the intention with NPM's wrapper invoking the script and calling exit $?? Why does it only cause problems when invoked using source? And how should I really go about to create a tool installed with npm that is invokable with source or . on both Windows (Git Bash) and Linux, exporting environment variables to the calling shell and without closing it?

这里发生了什么? NPM的包装器调用脚本并调用exit $的意图是什么?为什么它只在使用source调用时引起问题?我应该如何创建一个安装了npm的工具,该工具可以使用source或者调用。在Windows(Git Bash)和Linux上,将环境变量导出到调用shell而不关闭它?

1 个解决方案

#1


0  

The problem seems to be that git bash does not work as a regular console.

问题似乎是git bash不能作为常规控制台。

Either fix it as suggested in the link or use a full fledged bash terminal.

要么按照链接中的建议修复它,要么使用完整的bash终端。

#1


0  

The problem seems to be that git bash does not work as a regular console.

问题似乎是git bash不能作为常规控制台。

Either fix it as suggested in the link or use a full fledged bash terminal.

要么按照链接中的建议修复它,要么使用完整的bash终端。