斌脚本在Windows上工作正常,但在Linux上奇怪地失败

我正在尝试为Node编写一个小的CLI帮助程序,让我可以跨平台设置环境variables。 例如,如果您有像以下这样的NPM脚本:

"something": "MY_VAR=thing node index.js" 

在Windows上, MY_VAR将被取消设置。 我的解决scheme是编写一个小型库,它提供了一个新的命令,将运行一个给定的环境variables(在自述文件中的例子)的子命令。 我在Windows中testing和开发它,假设这将是更难的目标环境,并使其完美运行。 然而,当我然后试图在Linux上testing这个,我得到一个奇怪的错误。 我用这个package.json做了一个新的项目:

 { "scripts": { "grunt": "grunt --help", "envade": "envade" }, "dependencies": { "envade": "^1.0.1", "grunt-cli": "^0.1.13" } } 

接着:

 $ npm run envade > @ envade /code > envade : No such file or directory npm ERR! Linux 3.13.0-57-generic npm ERR! argv "node" "/usr/local/bin/npm" "run" "envade" npm ERR! node v0.12.6 npm ERR! npm v2.11.3 npm ERR! file sh npm ERR! code ELIFECYCLE npm ERR! errno ENOENT npm ERR! syscall spawn npm ERR! @ envade: `envade` npm ERR! spawn ENOENT npm ERR! npm ERR! Failed at the @ envade script 'envade'. npm ERR! This is most likely a problem with the package, npm ERR! not with npm itself. npm ERR! Tell the author that this fails on your system: npm ERR! envade npm ERR! You can get their info via: npm ERR! npm owner ls npm ERR! There is likely additional logging output above. npm ERR! Please include the following file with any support request: npm ERR! /code/npm-debug.log 

这里可能会出现什么问题? npm run grunt作品绝对好,而且据我所知,我遵循相同的应用程序结构。 我试图重新命名index.jsindex (没有文件扩展名),但同样的错误似乎发生。 我也尝试用console.log('hi');replaceindex.js的内容console.log('hi'); 在安装之后,排除脚本本身的错误。

我最初遵循本指南来创build节点脚本。 我发现这个错误很难谷歌,因为没有文件名!

问题是DOS / Windows换行(crlf)。 所以,'node ^ M'在你的envade二进制文件中被解释为这一行的一个文件名:

#!/usr/bin/env node^M

当然,该文件无法find…

PS您可以使用以下命令修复问题:

sed -i 's/\r//' envade