Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node script executable not working on Mac : env: node\r: No such file or directory

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

[...]
like image 490
PauloDev Avatar asked May 20 '15 08:05

PauloDev


4 Answers

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
like image 170
PauloDev Avatar answered Nov 15 '22 05:11

PauloDev


You could also use vim:

vim script

:se ff=unix
:wq

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

like image 42
JohnQ Avatar answered Nov 15 '22 05:11

JohnQ


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
like image 39
david_p Avatar answered Nov 15 '22 05:11

david_p


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

like image 30
drkvogel Avatar answered Nov 15 '22 04:11

drkvogel