简体   繁体   English

编写节点Shell脚本时找不到命令

[英]Command Not Found when writing a Node Shell Script

I am attempting to create a shell script using Node.js similar to packages like express and mocha . 我试图使用Node.js创建一个shell脚本,类似于expressmocha这样的包。 My initial test reported command not found so I backed the script to the following: 我的初始测试报告命令未找到,所以我将脚本备份到以下内容:

#!/usr/bin/env node

console.log("printing stuff");

The above should just output "printing stuff" when ran; 跑步时上面应该输出“打印东西”; however, that too doesn't work. 但是,这也行不通。

I'm attempting to execute the script via ./script in the directory of the file in order to ensure it's not a pathing issue. 我试图通过文件目录中的./script执行脚本,以确保它不是路径问题。 Attempting to execute under a privileged state (ie sudo) yields the same results/error message. 尝试在特权状态(即sudo)下执行会产生相同的结果/错误消息。

Any idea what I need to do? 知道我需要做什么吗?

When you shebang ( #! ) /usr/bin/env node , you are saying "look for a program called node on my PATH". 当你使用shebang( #!/usr/bin/env node ,你会说“在我的PATH上寻找一个名为node的程序”。 If doing node -v works, as @Ryan says, the shebang should work fine. 如果正在执行node -v工作,正如@Ryan所说,shebang应该可以正常工作。 Any chance you have a shell alias or shell function named node ? 有没有机会你有一个名为node的shell别名或shell函数? That could be giving you the illusion that your PATH is correct even when it is not. 这可能会让你觉得你的PATH是正确的,即使它没有。 But essentially, just make sure the directory where the node binary lives (the bin directory under your node install) is on your PATH environment variable, and things really should work at that point. 但实质上,只需确保node二进制文件所在的目录(节点安装下的bin目录)位于PATH环境变量上,并且事情确实应该在那时起作用。

export PATH=/usr/local/node/bin:$PATH

Figured out my issue. 想出了我的问题。 I made the shell script but forgot to set execute permissions onto the file. 我制作了shell脚本但忘了在文件上设置执行权限。 doing a chmod a+x script resolved the issue. 做一个chmod a+x script解决了这个问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM