24
votes

I have created my node script executable to execute some tasks grunt. On Windows, my node script works fine. But on Mac OS X (Yosemite), it's not working.

My node script has been published on Windows.

My node script is installed via npm command :

npm install -g task-app

My node script have this first line :

#! /usr/bin/env node

I tried many some solutions to solve my problem but I'm still stuck.

Here's these solutions that I used :

  1. uninstall and reinstall Node.js
  2. execute this command to create a link for node : sudo ln -s /usr/bin/nodejs /usr/local/bin/node
  3. set my path with this command : export PATH=$PATH:/usr/local/bin/node

Do you have other solutions to propose ?

EDIT :

the beginning of my script :

#! /usr/bin/env node

var grunt = require('grunt');

//Get parameters from command line
var args = process.argv.splice(2);

[...]
8
I had this problem when installing npmjs.com/package/json-mock-server on macos with yarn, but not with npm. In both installs, some of the files in the package have crlf endings, but it does not seem to matter when installed with npm. I will create a new question about this an link here.drkvogel

8 Answers

40
votes

After all, I found the solution to my problem.

As my node script file has been created on Windows, the file is DOS format (line endings in DOS format I think). So, I used a module which allow to converting a file to a unix format :

brew install dos2unix
sudo dos2unix /usr/local/lib/node_modules/task-app/src/task-app.js
16
votes

You could also use vim:

vim script

:se ff=unix
:wq

That will confirm DOS-style newlines to Unix-style newlines.

8
votes

There is a problem with newlines in your script. Make sure that #!/usr/bin/env node is followed by \n (unix style) instead of \r\n (windows/dos style). To fix that, use the tr command to remove \r's from your file:

cat your_script.js | tr -d '\r' > fixed_script.js
3
votes

This should no longer be a problem since npm@^5.4.0. npm will now auto-convert to the correct line endings. See https://github.com/npm/npm/issues/12371.

This is, however, still an issue in yarn: https://github.com/yarnpkg/yarn/issues/5480.

If you've come to this page because you've encountered this error when using yarn instead of npm, like I did, you might want to consider using npm instead of yarn. npm has most of yarn's best features these days, anyway (arguably).

2
votes

As PauloDev says above, this is a Mac/Windows line ending issue. To elaborate, if you are using nvm you'll need to locate your script first (in my case I'm using express-mvc-generator):

# install dos2unix
brew install dos2unix

# output the full path of your node version
which node 
>> /Users/<username>/.nvm/versions/node/v8.0.0/bin/node

# confirm the file path
cat /Users/<username>/.nvm/versions/node/v8.0.0/lib/node_modules/express-mvc-generator/bin/express

# convert the line endings
sudo dos2unix /Users/<username>/.nvm/versions/node/v8.0.0/lib/node_modules/express-mvc-generator/bin/express

# then run your script
1
votes

The carriage return inserted by MS-DOS is interpreted as part of the script interpreter name, which is the correct behavior for Un*x systems by the way. Hence, the system looks for a file /usr/bin/node\r instead of /usr/bin/node. As others have pointed out, npm now "fixes" the problem by stripping off the newline character which is a somewhat dubious behavior.

Executable files with a shebang line that has a DOS line ending are corrupt and must be fixed by the author and not by users, npm, or yarn. At the time of this writing, there is little reason to still use DOS line endings, even if you develop on Windows systems. But you should at least fix the files you produce before distributing them to the general public. See https://help.github.com/en/github/using-git/configuring-git-to-handle-line-endings for how to configure git to handle line endings correctly.

0
votes

The first command tells Git to never change line endings (in the future). Next, we refresh each repository by removing every file from Git's index and, finally, rewriting the Git index to pick up all the new line endings. This fixes the CRLFs that were introduced to your local file system when you cloned each repository.

Run this command:

git config core.autocrlf false
git rm --cached -r .
git reset --hard
0
votes

Reason: This is typically due to a difference in line endings, especially the difference in LF vs. CRLF . Unix systems like Linux and macOS use LF , the line feed character, for line breaks by default. Windows, on the other hand, is special and uses CR/LF , carriage return AND line feed character, by default. Ref: https://qvault.io/clean-code/line-breaks-vs-code-lf-vs-crlf/

Solution: For mac users, change CRLF to LF in that file in which the error occurred.