Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an environment variable to be used during "npm install"?

Our coorporate network is very closed down, so a normal method of:

npm install [email protected]

Doesnt work since it is being blocked by a proxy, we need to provide the parameter CYPRESS_INSTALL_BINARYin the following way with the help of cross-env (since we have mainly Microsoft environments here).

cross-env CYPRESS_INSTALL_BINARY='\\localserver\cypress\3.4.1\cypress.zip' npm install [email protected]

This is easy to do on first install, but the problem is that everyone on the team needs to run this command. And I want it to be possible to just type npm install and they will get all requirements automatically. This is extra obvious when we want to update the cypress package, since the binary url needs to change each time.

I tried to add a preinstall script to my package.json like so:

 "scripts": {
    ...
    "preinstall": "cross-env CYPRESS_INSTALL_BINARY='\\localserver\cypress\3.4.1\cypress.zip'",
    ...
  },

But it seems like that environment variable set by cross-env is "gone" after preinstall is finished and install begins, since cypress tries and fails to download from the web. I am okay with it being temporary, but it needs to persist over the install-command. Also seen solutions with .env files but none of those have support for the install step as far as I can see.

My current solution is to run the entire cypress installation in the preinstall step, and it works but seems uneccessary to run a double install each time.

So, what I am asking for, is a way to let a developer just run the following commands on a brand new computer and be done.

git clone ...
cd ...
npm install

How can I do that?

like image 849
Nings Avatar asked Aug 09 '19 13:08

Nings


1 Answers

Same situation on my side, except that I want to prevent the installation of cypress on the local machine.

Solved it by adding a .npmrc into the root of the project and adding to version control.


Contents of .npmrc:

CYPRESS_INSTALL_BINARY=0


Since the environment variable is used on install time, the solution with cross-env was not possible, since one cannot be sure that cross-env has been already installed.

Let me know whether it helped or you have already another solution.

like image 133
user12879795 Avatar answered Sep 23 '22 19:09

user12879795