Node script executable does not work on Mac: env: node \ r: no such file or directory

I created my node script executable for some grunt tasks. On Windows, my node script works fine. But on Mac OS X (Yosemite) it does not work.

My node script is published on Windows.

My node script is installed via the npm command:

npm install -g task-app 

My node script has this first line:

 #! /usr/bin/env node 

I tried many solutions to solve my problem, but I am still stuck.

These are the solutions I used:

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

Do you have other solutions?

EDIT:

beginning of my script:

 #! /usr/bin/env node var grunt = require('grunt'); //Get parameters from command line var args = process.argv.splice(2); [...] 
+11
command-line osx-yosemite
source share
5 answers

In the end, I found a solution to my problem.

Since my node script file was created on Windows, the file is a DOS format (line ending in DOS format, I think). So, I used a module that allows you to convert the file to unix format:

 brew install dos2unix sudo dos2unix /usr/local/lib/node_modules/task-app/src/task-app.js 
+27
source share

You can also use vim:

 vim script :se ff=unix :wq 

This will confirm DOS-style newlines for Unix-style newlines.

+9
source share

Your script has a problem with new lines. Make sure that #!/usr/bin/env node followed by \n (unix style) instead of \r\n (Windows / dos style). To fix this, use the tr command to remove \r from your file:

 cat your_script.js | tr -d '\r' > fixed_script.js 
+7
source share

As PauloDev says above, this is the end of line issue on Mac / Windows. To clarify, if you use nvm you need nvm to find your script (in my case, I use 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
source share

This should no longer be a problem since npm@ ^ 5.4.0. Now npm is automatically converted to the correct line endings. See https://github.com/npm/npm/issues/12371 .

This, however, is still a problem with yarn: https://github.com/yarnpkg/yarn/issues/5480 .

If you came to this page because you encountered this error when using yarn instead of npm, as I did, you might consider using npm instead of yarn. In any case, npm has the best yarn characteristics these days (possibly).

0
source share

All Articles