35

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);

[...]
1
  • 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. Commented Oct 12, 2018 at 16:17

9 Answers 9

52

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
Sign up to request clarification or add additional context in comments.

1 Comment

Fixed the issue on my mac El Capitan from one I started on my pc at home, a lifesaver!
19

You could also use vim:

vim script

:se ff=unix
:wq

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

Comments

11

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

1 Comment

I tried your command but it's not working when I execute my command again. I haven't newlines after the first line from my node script. Take a look in my first post.
6

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).

Comments

6

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.

Comments

4

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

Comments

3

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.

Comments

3

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

1 Comment

This helped me also to fix my issue in mac.
0

For me it was just due to running .js file directly. Actually i was running two of commands in a single script when observed carefully found that after running build command keyword node was missing to run the .js file this was my actual script

"update": "npm run build && ./script/updateTask.js"

after fixing, it became

"update": "npm run build && node ./script/updateTask.js"

1 Comment

I'm not sure this makes it executable as I can run my other executable files (can't remember how I made them executable, sorry) without node, but it did help me at least run my script.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.