问题
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 :
- uninstall and reinstall Node.js
- execute 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 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:
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
回答2:
You could also use vim:
vim script
:se ff=unix
:wq
That will confirm DOS-style newlines to Unix-style newlines.
回答3:
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
回答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
回答5:
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).
回答6:
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.
来源:https://stackoverflow.com/questions/30344858/node-script-executable-not-working-on-mac-env-node-r-no-such-file-or-directo