NPM: make package.json work under both Unix (Mac OSX) and Windows
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.
I have been thinking for a while, but I doubt there is any aesthetic solution using these tools, to get the desired effect.
If you are able to influence the change in make.js
, I would rather change this file to accept prod
or dev
as argument, example: node make.js --build=dev
. With default value, to ensure backwards compatibility.
Using only npm and not modifying make.js
, I could think of only running another JavaScript code, which would change environment variable, and then call make.js
.
That will look something like:
"build": "node middleman.js"
Middleman.js file could then use child_process or another module to set variable and execute node make.js
file.
If you do not want to create an extra file, you can then embed all the JavaScript inside the package.json using:
"build": "node -e 'my code'"
Be warned, that running "node -e 'process.env[\'NODE_ENV\']=\'dev\' && node make.js" will not work, as process.env
sets variable in local process, not global (i.e. does not export to the system).
Not the direct solution, but for sake of best practices, make it work different.