Use git and npm on windows with Git Bash

If you are used to develop on Linux and you have to suddenly switch back to Windows for a particular project, it can be really painful to use native tools like putty or power shell to develop.
Don’t worry, there are plenty of solutions to make things right.
For example, you could work on a Linux virtual machine inside your Windows.
Another solution (the one I chose) is to use Git Bash.
This setup has several advantages:

  • It requires no installation, which means it can be set up without admin rights
  • It is rather lightweight and easily packageable
  • You still have access to your windows filesystem via your command line

We are going to configure and package nodejs/npm inside the Git Bash to share it with every developers of your new team.

Get the softwares

First, get the desired softwares:

  • Download git portable edition and install it (ex: in C:\Applications\setenv\git)
  • Download node zip package and unzip it (ex: in C:\Applications\setenv\node)

Git portable edition comes with Git Bash included.

Set up your PATH

You are now ready to set up your shell!
In git Bash, a Linux like environement is simulated so you can access your Linux filesystem with Linux style paths.
For example, to print the content of C:\Applications\setenv you can type:

ls /c/Applications/setenv

Open git bash and type the following command:

export PATH=$PATH:/c/Applications/setenv/node

Type then node --version and npm --version to check that node and npm are available.
If not, check that you have the npm and node.exe files in C:\Applications\setenv\node.

Set up your proxies

In Windows companies, you’ll probably be protected behind a proxy which requires configuration as well. Find out what the address is following this process.

Then to configure the npm proxy, type the following lines:

npm config set proxy http://USERNAME:PASSWORD@proxy.address.com:8080
npm config set https-proxy https://USERNAME:PASSWORD@proxy.address.com:8080

To configure the git proxy, type the following lines:

git config --global http.proxy http://USERNAME:PASSWORD@proxy.address.com:8080
git config --global https.proxy https://USERNAME:PASSWORD@proxy.address.com:8080

To configure the node proxy, type the following lines:

export HTTP_PROXY=http://USERNAME:PASSWORD@proxy.address.com:8080
export HTTPS_PROXY=https://USERNAME:PASSWORD@proxy.address.com:8080

You should now be able to use git clone and npm install for your applications!

Use a configuration file

To reproduce this configuration when launching your terminal, you can create a custom configuration file in C:\Applications\setenv\git\etc\setenv.conf :

# Include node PATH
PATH=$PATH:/c/Applications/setenv/node

if [ -f "etc/setenv.var" ];
then
  source "etc/setenv.var"
else
  touch etc/setenv.var

  read -p "What is your proxy login? " PROXY_LOGIN
  echo "PROXY_LOGIN=\"${PROXY_LOGIN}\"" >> etc/setenv.var

  read -p "What is your proxy password? " PROXY_PASSWORD
  echo "PROXY_PASSWORD=\"${PROXY_PASSWORD}\"" >> etc/setenv.var

  PROXY_ADDRESS="proxy.address.com"
  echo "PROXY_ADDRESS=\"${PROXY_ADDRESS}\"" >> etc/setenv.var

  PROXY_PORT="8080"
  echo "PROXY_PORT=\"${PROXY_PORT}\"" >> etc/setenv.var
fi

# Used for Node Terminal proxy (for packages as shipit)
export HTTP_PROXY=http://${PROXY_LOGIN}:${PROXY_PASSWORD}@${PROXY_ADDRESS}:${PROXY_PORT}
export HTTPS_PROXY=https://${PROXY_LOGIN}:${PROXY_PASSWORD}@${PROXY_ADDRESS}:${PROXY_PORT}
# Npm proxy
npm config set proxy $HTTP_PROXY
npm config set https-proxy $HTTPS_PROXY
# Git proxy
git config --global http.proxy $HTTP_PROXY
git config --global https.proxy $HTTPS_PROXY

Edit your etc/profile git bash file (Windows location: C:\Applications\setenv\git\etc\profile) and add the following line just before exporting the PATH:

source "etc/setenv.conf"

Reload your bash terminal, you will be asked for your proxy username and your proxy password.
This will create a setenv.var file which will be used when opening the git bash next time.
You should then be able to use npm and node out of the box!
For example, if you want to contribute to this planning burndown chart project:

git clone git@github.com:theodo/planning-bdc.git && cd planning-bdc
npm install
npm run watch

And here you go!

Package and share

You can now zip your C:\Applications\setenv folder into a setenv.zip file.
It will look like:

setenv
├─ git
│  ├─ git_bash.exe
│  └─ etc
│     ├─ profile (edited)
│     └─ setenv.conf
└─ node
   ├─ node
   └─ npm

To use the setenv, the other developers will have to:

  • Download the setenv.zip file
  • Unzip it in the C:\Applications folder
  • Put a shortcut to the git_bash.exe on their Desktop
  • Open the bash terminal
  • Fill the proxy’s username and password

And your shell is configured.

Conclusion

Now you got a configured and packaged Git Bash, you can adapt it to create your own environement!
Thanks for reading, don’t hesitate to leave feedback!


You liked this article? You'd probably be a good match for our ever-growing tech team at Theodo.

Join Us