M-Zuber/npm-watch

Line endings break cli.js on Unix Machines

Closed this issue · 10 comments

jcpst commented

After I upgraded a project to npm-watch@0.1.2, it was blowing up on me with the following error:

> npm-watch

/usr/bin/env: ‘node\r’: No such file or directory

npm ERR! Linux 4.4.5-1-ARCH
npm ERR! argv "/home/joe/.nvm/versions/node/v4.4.1/bin/node" "/home/joe/.nvm/versions/node/v4.4.1/bin/npm" "run" "watch"
npm ERR! node v4.4.1
npm ERR! npm  v3.8.3
npm ERR! file sh
npm ERR! code ELIFECYCLE
npm ERR! errno ENOENT
npm ERR! syscall spawn
npm ERR! supwithnpm-watch@1.0.0 watch: `npm-watch`
npm ERR! spawn ENOENT

So I cloned this repo, did a npm link and it worked fine, giving me the suspicion that this was a line ending issue.

I then installed npm-watch in a local project, it failed with the same issue. After running the following one-liner to clobber the Windows line-endings:

sed 's/\r$//' -i node_modules/npm-watch/cli.js

It worked fine again. This puzzled me because all I saw in the git history was the removal of that console.log().

I did a little research, and this is an open issue with npm and cross-platform compatibility: npm/npm#2097

So if you published from a Windows machine, npm publish sends the \r\n, but then git commit is converting the line endings and commiting \n, so you never see it in the repo.

So a little story: I HATE THIS LINE ENDING BUSINESS

Rant over, sorry about that.

I am mainly a windows guy, so correct me if I am wrong - Currently the only way to avoid this issue is by publishing from a linux machine?

jcpst commented

I agree, it would be nice if npm's 'publish' command just took care of it in a cross-platform way. But! I have an idea.

There is a module called crlf that will convert \r\n to \n

I just smoke-tested it and it appears to work fine.

If you create a prepublish lifecycle script and use it in there, that should take care of it before every time you publish.

{
  "scripts": {
    "prepublish": "crlf --set=LF cli.js"
  }
}

Or do it manually.

Or something else similar that uses that prepublish hook.

In theory a .gitattributes file (plus maybe a commit that fixes the current issue) should keep it from ever happening again.

Note - right now you have text=auto you probably want text eol=lf otherwise it's going to tank on 2/3 platforms (and the most common node platforms)

You can also change most text editors to have Unix style (linefeed only) line endings like OSX and all Linux flavors use.

I merged the fix for the .gitattributes file, but I think I will still add the prepublish hook as I do not want to rely on npm honoring the file.

ok, I've pushed v0.1.13.
@jcpst @abritinthebay if you could double check it for me when you get a chance and let me know if it worked

jcpst commented

Works! Thank you @M-Zuber

Yup, works great now. the prepublish hook makes sense - can't hurt to make sure

Thank you both for the help in diagnosing