Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NPM: make package.json work under both Unix (Mac OSX) and Windows

I've gotten some NPM package from a third party who is developing under Mac OSX. Their build can split into either development or production using "scripts" object in package.json. For example:

 "scripts": {
    "build": "NODE_ENV=dev node make.js --build",
    "build-prod": "NODE_ENV=prod node make.js --build",
  }

Under Unix, one can run either "npm run build" or "npm run build-prod" to build either directory (naturally, there are some conditional statements in make.js). Of course, it does not work under Windows - I had to change the commands similar to this:

 "scripts": {
    "build": "set NODE_ENV=dev&& node make.js --build",
    "build-prod": "set NODE_ENV=prod&& node make.js --build",
  }

(Please note that it was important not to put a space before the '&&' - otherwise the environment variable was created with an extra white space in it which ruined all those comparisons in make.js).

However, I would like to have some universal source tree which would work under either Unix or Windows without editing. Could you please give some ideas on how to conditionally split the build depending on the OS?

like image 534
Zarin Avatar asked Jul 08 '14 15:07

Zarin


1 Answers

The question is pretty old, but for these who faces the problem nowadays, npm starting from version >=5.1.0 supports setting shell for processing scripts. By default on Windows, npm internally uses cmd.exe for running scripts, even if npm command itself is typed in git-bash. After setting git-bash as shell, scripts that use bash syntax work normally on Windows:

npm config set script-shell "C:\\Program Files\\Git\\bin\\bash.exe"

Here one needs to substitute his correct path to git-bash executable.

like image 136
Vasiliy Avatar answered Oct 13 '22 01:10

Vasiliy