带有nodejs的CLI [英] CLI with nodejs

查看:69
本文介绍了带有nodejs的CLI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在节点中开发CLI,该节点将发布到NPM。
因为它是一个CLI应用程序,所以我希望它在安装后就包含在路径中,因此不需要键入 node my-app.js即可运行它。我希望它仅与 my-app一起运行。

I'm developing a CLI in node that will be published to NPM. Since it's a CLI application, I want it to be included in the path once it's installed, so it's not require to type "node my-app.js" to run it. I want it to run with only "my-app".

在package.json中,我包括:

In the package.json, I'm including:

"bin": { 
    "my-all" : "./my-app.js" 
 },

但这会导致通过NPM安装失败,并出现此错误

But this makes fail the installation via NPM with this error

错误:ENOENT,chmod'/ home / user1 / node_modules / my-app / my-app'

推荐答案

假设您使用的是某种类型的Unix(Linux,OSX),请将此行放在脚本顶部:

Assuming you're on some kind of unix (linux, osx), put this line at the top of your script:

#!/usr/bin/env node

还要确保将文件设置为可执行文件( chmod a + x my-all )。

Also make sure you set the file to executable (chmod a+x my-all).

这应该满足键入的需要节点my-app.js ,然后使您只需键入 ./ my-app.js

That should take care of the need to type node my-app.js, and enable you instead to just type ./my-app.js.

对于npm包装的东西,我不确定为什么会失败,但是我猜测这与my-app.js的路径或位置有关。

As for the npm packaging stuff I am not sure why it fails, but I'm guessing it's an issue with the path or location of your my-app.js .

如果是可执行文件脚本放在PATH中的任何位置,然后它将像其他任何东西一样运行。如果运行哪个npm ,您将看到npm可执行脚本所在的位置。在我的系统上,大多数节点可执行文件(或可执行npm脚本)进入/ usr / local / bin。我假设您的 package.json 可以设置为放置在路径中。如果需要更改路径,请修改 .profile 或系统路径。

If an executable script is put anywhere in the PATH, then it will be run just like anything else. If you run which npm, you will see where the npm executable script is located. On my system, most node executable (or executable npm scripts) goes into /usr/local/bin. I'm assuming your package.json can be set to put it somewhere in the path. If you need to change the path, then modify your .profile, or alternatively your system path.

这篇关于带有nodejs的CLI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆