简体   繁体   English

npm二进制+ Windows git bash +环境变量

[英]npm binary + windows git bash + environment variables

In my node module I've created a tool that is installed globally using npm i -g . 在节点模块中,我创建了一个使用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. 该工具是一个bash脚本,用于设置我的本地配置,调用其他一些脚本,并且还需要设置一些环境变量。 On Linux, it works fine calling 在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. 这将调用my-setup ,并且由于使用“ dot”或source进行调用,因此脚本设置的环境变量将导出到调用shell。 No problems. 没问题。

However, when running the same script on Git bash in Windows 7, the script is invoked, but afterwards the entire shell is closed. 但是,当在Windows 7中的Git bash上运行相同的脚本时,将调用该脚本,但随后关闭整个外壳。 When looking into how NPM treats globally installed binaries in Windows, I noticed 当研究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. 因此,我怀疑这里的最终退出语句是导致在Windows上采购此脚本时关闭调用外壳的原因。 If I manually do 如果我手动做

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

Then the script is executed fine and variables exported properly, while not closing the shell. 然后,在不关闭外壳程序的情况下,脚本可以很好地执行并正确导出变量。

What's going on here? 这里发生了什么? What is the intention with NPM's wrapper invoking the script and calling exit $? NPM的包装程序调用脚本并调用exit $? ? Why does it only cause problems when invoked using source ? 为什么只有在使用source调用时才引起问题? And how should I really go about to create a tool installed with npm that is invokable with source or . 以及我该怎样真正创建一个与npm一起安装的,可以与source. on both Windows (Git Bash) and Linux, exporting environment variables to the calling shell and without closing it? 在Windows(Git Bash)和Linux上,是否将环境变量导出到调用shell而不关闭它?

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终端。

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

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