-
Notifications
You must be signed in to change notification settings - Fork 3k
npm run-script
doesn't work on windows when the script contains multiple commands
#4040
Comments
Hrmmm, running the joined commands via
|
npm script runs are handled on linux/osx by calling sh so I would imagine on windows it is done by calling cmd.exe (or whatever your environment has defined) and as such I can't imagine you could expect ; to work across all OS's even on linux I have trouble getting complex command lines to survive the journey thru nodejs and npm's script runs - so I just got used to creating wrapper bash scripts and letting the shebang line tell sh that it needs to spawn bash i'm wondering if you can do something similar - create a foo.sh script and see if powershell's linux-like shell handling lets you get your commands run |
Commands are passed verbatim to either Keep your command lines simple. A bash script won't work on Windows, for obvious reasons. Write a node program, or better yet, just don't use an install command. Presumably your prepublish script could just be written as a Node program, and run Also, never ever write ./node_modules/.bin/ in your scripts. Seriously, that should just throw. What if the dep is somewhere else, installed globally, or as a parent's dep? npm puts |
So the only case when |
I want to both run bower install and update my webdriver during postinstall; with the same package.json usable on both windows and linux. Is there way that will let both these scripts run without making a new file? Here's what I am doing now: The relative paths are because I don't want to assume bower or protractor are installed globally. |
@balupton, this should work:
The only 'catch' is that the second command ( |
@maranomynet the |
oh, I assumed the semi-colon was part of his problem, because on Windows command-line semicolon delimits parameters, instead of operations. I stumbled upon this post as I was dealing with two-step package.json scripts not running on Windows, because of this semi-colon issue. As soon as I changed the semi-colon to & (windows-only replacement for ;) or && (conditional) everything started running smoothly. And && works cross-platform. |
- When running npm scripts, npm puts ./node_modules/.bin in the path so referencing hard coded ./node_modules/mocha/bin/mocha is not recommended nor is it cross platform. See npm/npm#4040 (comment) - Updated mocha to 2.3.4
&& do not work on Powershell |
npm does not run package scripts using PowerShell on Windows, @zougi. |
Hi @othiym23. I did a re-test, you are right. I thought npm uses the shell from where it is called. Thanks for the clarification. |
See issue npm/npm#4040, comment by @othiym23 npm-run-script adds ./node_modules/.bin on the PATH
See issue npm/npm#4040, comment by @othiym23 npm-run-script adds ./node_modules/.bin on the PATH
FWIW I just do this in my npm scripts to get them to work on windows AND linux/mac:
|
sh is not recognized on Windows |
@Noxxys, which version of windows? I was only using a Windows 10 server to test. |
I'm using Windows 10, but it will be the same on Windows 7 and 8.1. sh is a Unix program, not installed on Windows unless you use a special shell. |
@usergenic: Thanks for the tip. |
wouldn't https://msdn.microsoft.com/en-us/commandline/wsl/install_guide solve this without having to change anything in our packages? for an update from my side, I've just gone and used npm scripts for everything https://github.com/bevry/base/blob/7f88b7dd5994d0637595b39f92bb168bc82695b9/package.json#L85-L105 with bash conventions - and now that every OS that devs use has bash, I'm happy to just say to our devs use bash when running the npm scripts - I haven't yet tried this on windows, but I'd imagine it would work - it works on non-standard login shells like fish on mac and linux (fish doesn't use If it doesn't work on windows due to:
Then it would be good if npm preferred bash or sh over |
I'm dealing with the same issue. "&&" is what I'd normally use on windows, but it's not supported on POSIX. With Windows 10 1703 and WSL, I can use the sh trick that @usergenic mentioned, but my Windows Servers aren't running WSL so I'm still looking for a solution. Why not just allow scripts (in package.json) to be string OR Array and when an array, parse it and execute each? |
Essentially, this is boolean logic. So you can use that boolean logic to force the second script to run, even if the first script fails, and yes it works on Windows (where the semi-colon syntax doesn't work). Here's what I originally had in the
In this case,
Now I had the opposite problem: The way to force
Applying that logic to my package.json script:
By wrapping he first script call with in some brackets and the ORing it with a It works for me on Windows anyway. |
If I have:
And I run
npm run-script prepublish
on windows, no script will execute.If I run the scripts manually, it works:
The text was updated successfully, but these errors were encountered: