Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix postinstall npm script on Windows #73

Closed
bryaneaton13 opened this issue Sep 28, 2015 · 17 comments
Closed

Fix postinstall npm script on Windows #73

bryaneaton13 opened this issue Sep 28, 2015 · 17 comments

Comments

@bryaneaton13
Copy link

When installing, the build step complains that
'babel' is not recognized as an internal or external command, operable program or batch file.

I installed babel as a dev dependency in my project and then history was able to install correctly.

Could the build step be changed to run babel from the node_modules instead?

(I'm on Windows btw)

@bebraw
Copy link

bebraw commented Sep 29, 2015

Looks like f4623ba introduced a postinstall script. That in turn invokes a Babel based build. This change was needed by #52. The problem is that if you rely on history through git, it won't contain a correct build (generated by prepublish).

I suppose ideally it shouldn't trigger postinstall if you are treating history as a regular dependency. If it's possible to detect how it was installed (from git or in a normal way), then you can skip Babel build altogether (it will point at lib in this case as it should).

@mjackson
Copy link
Member

mjackson commented Oct 1, 2015

Are you installing from a git repo, @bryaneaton13?

@bryaneaton13
Copy link
Author

@mjackson I'm using npm to install it. npm i history -D
It works when I already have babel installed locally or globally, but on a fresh project install (no babel installed anywhere) with history as a dependency, I get this error.

@bebraw
Copy link

bebraw commented Oct 1, 2015

@mjackson The problem is that postinstall, and hence Babel, gets triggered in a regular install as well for some reason. I can see it has a guard in place

node -e \"require('fs').stat('lib', function (e, s) { process.exit(e || !s.isDirectory() ? 1 : 0) })\"

I suppose the question is why that guard might fail in this case. Why else would it try to trigger Babel unless this check fails?

@bryaneaton13
Copy link
Author

I get an error on that line also before I get the babel error message.

image

@bebraw
Copy link

bebraw commented Oct 1, 2015

@bryaneaton13 Can you provide more info? Which version of Node are you using? Do you get the same error when running the node -e ... script standalone?

@bryaneaton13
Copy link
Author

node -v v0.10.21
npm -v 1.3.11
If I copy and paste that command without || npm run build, it completes.

@taion
Copy link
Contributor

taion commented Oct 8, 2015

@bryaneaton13 Are you running on Windows?

@bryaneaton13
Copy link
Author

@taion Yep

@taion
Copy link
Contributor

taion commented Oct 8, 2015

I think that's the issue - I believe you can't do cmd1 || cmd2 on Windows.

@mjackson
Copy link
Member

mjackson commented Oct 8, 2015

I guess that means cmd1 && cmd2 won't work on Windows either, which means our npm test script is probably broken as well. Nice debugging, @taion.

Now that we know what the problem is, anyone want to make a PR?

@mjackson mjackson changed the title 'babel' not recognized on install Fix postinstall (and test) npm scripts on Windows Oct 8, 2015
@taion
Copy link
Contributor

taion commented Oct 8, 2015

cmd1 && cmd2 actually does work on Windows. It's just the || that doesn't work. ¯_(ツ)_/¯

@mjackson mjackson changed the title Fix postinstall (and test) npm scripts on Windows Fix postinstall npm script on Windows Oct 9, 2015
mjackson added a commit that referenced this issue Oct 9, 2015
@mjackson
Copy link
Member

mjackson commented Oct 9, 2015

ok, @bryaneaton13. I think if we use && instead of || your problem should go away. Can you please try installing from the fix-postinstall-windows branch?

npm install rackt/history#fix-postinstall-windows

@taion
Copy link
Contributor

taion commented Oct 9, 2015

I think that might break installing when the built files are present, though, since you'd be exiting with a non-zero status code. Per https://docs.npmjs.com/misc/scripts#best-practices:

Don't exit with a non-zero error code unless you really mean it. Except for uninstall scripts, this will cause the npm action to fail, and potentially be rolled back.

@mjackson
Copy link
Member

mjackson commented Oct 9, 2015

Bah, good point. Any ideas? Last time I wrote code for CMD.exe was at least
a decade ago.

On Thu, Oct 8, 2015 at 5:53 PM Jimmy Jia notifications@github.com wrote:

I think that might break installing when the built files are present,
though, since you'd be exiting with a non-zero status code. Per
https://docs.npmjs.com/misc/scripts#best-practices:

Don't exit with a non-zero error code unless you really mean it. Except
for uninstall scripts, this will cause the npm action to fail, and
potentially be rolled back.


Reply to this email directly or view it on GitHub
#73 (comment).

@taion
Copy link
Contributor

taion commented Oct 9, 2015

I'm not super familiar with child_process, because I don't script in Node, but this seems to work:

-    "postinstall": "node -e \"require('fs').stat('lib', function (e, s) { process.exit(e || !s.isDirectory() ? 1 : 0) })\" || npm run build"
+    "postinstall": "node -e \"require('fs').stat('lib', function (e, s) { if(e || !s.isDirectory()) require('child_process').spawn('npm', ['run', 'build']) })\""

ETA: I mean it works in Bash, and I think it should work on Windows.

@mjackson
Copy link
Member

mjackson commented Oct 9, 2015

Ya, I think you're right. Let's try that.

@lock lock bot locked as resolved and limited conversation to collaborators Jun 5, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants