简体   繁体   中英

npm binary + windows git bash + environment variables

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

$ . 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.

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

$ 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

$ . "$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 $? ? 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?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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