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

前端 未结 6 1145
清酒与你
清酒与你 2021-01-31 19:10

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 scri

相关标签:
6条回答
  • 2021-01-31 19:29

    You could also use vim:

    vim script
    
    :se ff=unix
    :wq
    

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

    0 讨论(0)
  • 2021-01-31 19:30

    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 讨论(0)
  • 2021-01-31 19:37

    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
    
    0 讨论(0)
  • 2021-01-31 19:39

    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
    
    0 讨论(0)
  • 2021-01-31 19:45

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

    0 讨论(0)
  • 2021-01-31 19:53

    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
    
    0 讨论(0)
提交回复
热议问题